【发布时间】:2021-08-24 01:25:12
【问题描述】:
我需要发出一个 http 请求并在成功响应时创建并插入一条记录。这是代码的sn-p
public static Collaboration__c createGroupCollaboration(Sub_Folder__C folder, String groupId, string role, String ProfId){
Box__c box = Box__c.getInstance();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://test.api');
req.setMethod('POST');
req.setHeader('Authorization','Bearer '+box.BoxAccess_Token__c);
req.setBody('{"item": { "id": "'+folder.CF_box_folder_id__c+'", "type": "folder"}, "accessible_by": { "id": "'+groupId+'", "type": "group" }, "role": "'+role+'"}');
HTTPResponse res = new Http().send(req);
if(res.getStatuscode() >= 200 && res.getStatusCode() <= 299){
Collaborationwrapper cw = new Collaborationwrapper();
cw = (Collaborationwrapper)JSON.deserialize(res.getBody(),Collaborationwrapper.class);
Collaboration__c resCollab = new Collaboration__c(BoxCollaboration_Id__c = cw.Id,Role__c= cw.role,BoxFolder_Id__c= folder.CF_box_folder_id__c,Sub_folder__c= folder.id,BoxGroup_Id__c=groupId,SFUser_Email__c = 'group@test.com',SFProfile_Id__c = ProfId);
return resCollab;
}else {
return null;
}
}
这个类返回一个将被插入的协作。当我评论插入时,一切正常。但是,当我启用插入时,我收到以下错误:您有未提交的工作待处理。请在调用前提交或回滚。
只有在操作成功时才需要插入这些记录。
我也需要为删除执行此操作,但我认为相同的方法将适用于两个操作。
我知道您不应该在标注后执行 DML...但是如果成功响应,我还能如何创建记录?
【问题讨论】:
标签: salesforce apex