Memos About Salesforce

Salesforceにハマってたこと!

zipCloud 使い方 郵便番号検索機能 注意点

こんにちは、管理人の@Salesforce.Zです。

郵便番号検索機能をいうと

やっぱり【zipCloud】かも

APIを提供しているからし、企業として提供、少し信頼が高い

しかし、1点問題がある

それがなにかというのを

今回、共有したいと思います。

欲しけりゃくれてやる・・・。

探せ!

この世の全てをそこに置いてきた〜笑

目次

Salesforceでコールする方法

ZipCloudのAPI

http://zipcloud.ibsnet.co.jp/api/search

となっています。

このAPISalesforceのVF側でjavaScriptでコールしても

結果が返ってこない、【Get.........()0】エラーになる

原因がsalesforceのVF側でjsを用いてコールしたら

https://zipcloud.ibsnet.co.jp/api/search

というセキュア通信になってしまうためです。

だから、Apex側でコールするしかない

Apex側でコールし結果をそのまま、結果を処理

シンプルサンプル

public with sharing class SearchByPostZip{
        // @postalCD 検索したい郵便番号
        // @return jsonの文字列
        public static String getJSONByPostCD(String postalCD){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        String url = 'http://zipcloud.ibsnet.co.jp/api/search?callback=?&zipcode=' + postalCD;
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setEndpoint(url);
        req.setMethod('GET');
        req.setTimeout(20000);
        Boolean isError = false;
        try{
            res = http.send(req);
        }catch(System.CalloutException ce){
            isError = true;
            return ce.getMessage();
        }
        return res.getBody();
        }
}
Apex側でコールし結果をVFのjsに渡す、jsなどで処理

Globalクラスの中にRemoteActionメソッドを用意しjSon文字列を返す

結果をVF側で受け取って処理可能

サンプルクラス

global with sharing class SearchByPostZip{
  @RemoteAction
    global static String searchBnt(String postzip){
            String result = '';
            //String postzip = ApexPages.currentPage().getParameters().get('postzip');
            System.debug('★★★対象郵便番号 : ' + postzip);
            result = getJSONByPostCD(postzip);
            String resultReplace1 = result.replace('?', '');
            String resultReplace2 = resultReplace1.replace('(', '');
            String resultReplace3 = resultReplace2.replace(')', '');
            System.debug('★★★結果-2★★★' + resultReplace3);
            return resultReplace3;
        }

        // @postalCD 検索したい郵便番号
        // @return jsonの文字列
        public static String getJSONByPostCD(String postalCD){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        String url = 'http://zipcloud.ibsnet.co.jp/api/search?callback=?&zipcode=' + postalCD;
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setEndpoint(url);
        req.setMethod('GET');
        req.setTimeout(20000);
        Boolean isError = false;
        try{
            res = http.send(req);
        }catch(System.CalloutException ce){
            isError = true;
            return ce.getMessage();
        }
        return res.getBody();
    }
}

サンプルVF一部抜粋

<script type="text/javascript">
            function methodSample(zipcode){
                //VF側でAPEX側のRemoteAction methodをコールし文字列がresultに返す
                {!$RemoteAction.SearchByPostZip.searchBnt}(zipcode, function(result, event){
                            if(event.status) {
                                 //処理:文字列解析が必要
                            }
                });
            }
           
</script>

まとめ

zipCloudはセキュア通信、ダメよ

結果が返ってこない