高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@RestResource(urlMapping='/OFSCustomSetting/*')
global class OFSCustomSettingRest {
    /**
     * カスタム設定取得
     */
    @HttpGet
    global static void doGet() {
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        
        String jsonResponse;
        List<OFSSetting__c> ofsList = OFSSetting__c.getall().values();
        if (ofsList == null || ofsList.isEmpty()) {
            res.statusCode = 200;
            jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage +'": "No found OFSSetting__c"}';
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        } else {
            try {
                res.statusCode = 200;
                for (OFSSetting__c ofs : ofsList) {
                    if (String.isBlank(ofs.sqlWhere__c) == false) {
                        ofs.reportId__c += ' ' + ofs.sqlWhere__c;
                        ofs.sqlWhere__c = null;
                    }
                }
                jsonResponse = Json.serialize(ofsList);
                // TODO ここだけです。OFSErrorStatus などや設定しない、直接データとして戻す
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            } catch ( Exception ex ) {
                res.statusCode = 200;
                Map<String, String> forJson = new Map<String, String>();
                forJson.put(System.Label.OFSErrorStatus, System.Label.OFSErrorFailure);
                forJson.put(System.Label.OFSErrorMessage, '' + ex);
                jsonResponse = Json.serialize(forJson);
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            }
        }
    }
    
}