我正试图从salesforce发出一个电话,大部分代码都是从另一个工作包中复制的。
有人能告诉我为什么下面的call方法从未运行过吗?
在调用方法之前和之后,我将保存到自定义表中,但在call out方法中不会调用对自定义表的保存。
公共类AutoSyncConnector {
public AutoSyncConnector()
{
}
public void Fire(string jsonToPost)
{
// 1. Authentication send the current session id so that request can be validated
String sessionId = UserInfo.getSessionId();
// 2. warp the request and post it to the configured end point
// This is how to get settings out for custom settings list
String connectorUrl = ASEndPoints__c.getValues('MainUrlEndPoint').autosync__MainSiteUrl__c;
CastlesMessageLog__c cd = new CastlesMessageLog__c();
cd.SentJson__c = 'before call out this is called';
insert cd;
AutoSyncConnector.CallOut(jsonToPost, connectorUrl);
CastlesMessageLog__c cd2 = new CastlesMessageLog__c();
cd2.SentJson__c = 'after call out this is called';
insert cd2;
}
public static void CallOut(String jsonToPost, String connectorUrl)
{
MakeCallout(jsonToPost,connectorUrl);
}
@future(callout=true)
public static void MakeCallout(String jsonToPost, String connectorUrl){
CastlesMessageLog__c cd = new CastlesMessageLog__c();
cd.SentJson__c = 'start inside before call out this is never called';
insert cd;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setTimeout(120000);
// string authorizationHeader = 'Check I can add stuff to the header';
String sfdcConnectorUrl = connectorUrl + '/api/autosyncwebhook';
req.setEndpoint(sfdcConnectorUrl);
//req.setHeader('Authorization', authorizationHeader);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setBody(jsonToPost);
h.send(req);
CastlesMessageLog__c cd2 = new CastlesMessageLog__c();
cd2.SentJson__c = 'end inside before call out this is never called';
insert cd2;
}}
发布于 2017-10-06 12:11:24
转到安装->监视-> Apex作业。我的直觉是,你会看到很多“未完成的工作等待”的错误在那里。
创建任何DML (插入/更新/删除)时,都会使用数据库打开事务。如果您要做的下一件事是一个标注(它可以有120秒的最大超时时间),这将意味着您在这个记录(甚至整个表)上持有一个锁很长时间。SF无法知道呼叫是否会成功或必须回滚。因此,他们立即禁止这样的法规来保护这种情况;)
先做标注,然后是你的DML。
或者让DML调用@未来(这就是目的,切换到另一个线程,分离上下文),如果标注返回错误--做任何你认为回滚的清理(删除记录?是否将其更新为状态=同步失败?发送电子邮件给用户/为他插入一个任务,稍后再试?)
https://stackoverflow.com/questions/46604221
复制相似问题