0

我的代码基本上做我想做的事情,直到我想将响应 ID 写回 Salesforce 的部分。

public class ExpensifyCallout {
    //@future(callout=true)
    public static void createReport(Id SFReportID, String projectName, Date projectStartDate, Date projectEndDate, String projectReportLocation, String policyID, String employeeEmail){

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:Expensify_API');    
        request.setHeader('ContentType', 'application/json');    
        request.setBody('requestJobDescription={"type": "create","credentials": {"partnerUserID": "{!$Credential.Username}" ,"partnerUserSecret": "{!$Credential.Password}"},"inputSettings": {"type": "report","policyID": "'+policyID+'","report": {"title": "'+projectName+'/ '+projectStartDate.format()+' - '+projectEndDate.format()+'/ '+projectReportLocation+'"},  "employeeEmail": "'+employeeEmail+'", "expenses":[]}}');
        request.setMethod('POST');
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
           System.debug(response.getBody());
           Map <String, Object> responseJSON = (Map <String, Object>) JSON.deserializeUntyped(response.getBody());
           String reportID = string.valueof(responseJSON.get('reportID'));
           System.debug('Hello World! ');
           System.debug('Hello World! '+ reportID);

           /*ExpensifyReport__c[] eRecords = [SELECT Name
                                   FROM ExpensifyReport__c
                                   WHERE Id = :SFReportID];
           ExpensifyReport__c e = eRecords[0];
           e.expensifyReportID__c = reportID;*/
        }
        // else handle problem
    }

我的 api 响应代码如下: {"reportID":"54995495","reportName":"My TEST report","responseCode":200} 我想检索 reportID 并将其写回我创建的触发标注的记录。

只有两个带有 Hello World 的语句不会被打印出来。其余的按预期工作。有谁知道为什么会这样?非常感谢。

我注意到一条对我没有多大帮助的错误消息: Invalid api version:0.0 当我签入设置时,列出的 api 都没有 0.0 版...

4

1 回答 1

0

我已经通过以下测试验证了您的代码运行良好:

HttpResponse response = new HttpResponse();
response.setBody('{"reportID":"54995495","reportName":"My TEST report","responseCode":200}');

Map <String, Object> responseJSON = (Map <String, Object>) JSON.deserializeUntyped(response.getBody());
String reportID = string.valueof(responseJSON.get('reportID'));
System.debug('The reportId is: '+ reportId); 

您的 API 版本问题可以通过以下方式解决:设置 -> Apex 类 ->单击 Apex 类-> 编辑 -> 版本设置 -> 将 API 版本更改为当前值以外的任何内容。-> 将其更改回适当的 API 版本。

于 2019-12-17T01:42:48.910 回答