You need to sign in to do that
Don't have an account?
![RIteshM RIteshM](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
System.CalloutException: You have uncommitted work pending. Please commit or rollback
My CreateTimeLine code is
public static void createTimeLine(List<sObject> objList,Map<String,String> contentMap){
Map<Id,User> userMap = new Map<Id,User>([Select id,Name From User where Authorize__c = true]);
Map<Id,GlassUserApiSettings__c> userSettingsMap = new Map<Id,GlassUserApiSettings__c>();
for(Id user_id : userMap.keySet()){
GlassUserApiSettings__c userSettings = GlassUserApiSettings__c.getValues(user_id);
if(userSettings == null)
{ User u =userMap.get(user_id);
u.Authorize__c = false;
update u;
}
else userSettingsMap.put(user_id,userSettings);
}
for(sObject obj : objList)
for(Id userId: userSettingsMap.keySet() ){
String type = '';
if(contentMap.get('Object').equals('FeedItem'))
type='Post';
if(contentMap.get('Object').equals('FeedComment'))
type= 'Comment';
GlassUserApiSettings__c userSettings = userSettingsMap.get(userId);
if(((String)obj.get(contentMap.get('Content'))).contains('@'+userMap.get(userId).Name)){
Datetime tokenExpiredTime = userSettings.LastModifiedDate;
tokenExpiredTime.addSeconds(Integer.valueOf(userSettings.ExpireDuration__c));
String body='{"html":"<article><section><div class="text-auto-size">'+type+':'+
'<p class="yellow">"'+obj.getSObject('CreatedBy')+' '+obj.getSObject('CreatedBy')+'</p><p>'+obj.get(contentMap.get('Content'))+'</p>'+
'</div></section> </article>"}';
System.debug('Body is '+body);
/* if(tokenExpiredTime >= System.now()){
GMirror.TokenResponse res = GMirrorUtil.refreshToken(userSettings.RefreshToken__c);
userSettings.RefreshToken__c = res.refresh_token;
userSettings.AccessToken__c = res.access_token;
userSettings.ExpireDuration__c = Integer.valueOf(res.expires_in) ;
update userSettings;
}*/
String timelineRes = doApiCall(body,'POST','https://www.googleapis.com/mirror/v1/timeline',userSettings.AccessToken__c);
GMirror.TimelineResponse createdTimeCard = (GMirror.TimelineResponse) JSON.deserialize(timelineRes,GMirror.TimelineResponse.class);
if(createdTimeCard.id != null)
System.debug('created timeline card :'+createdTimeCard);
else { try{
throw new GMirror.TimelineException(null,timelineRes);
}
catch(Gmirror.TimelineException e){
System.debug(e.getMessage());
}
}
}
}
}
and My Batch apex code is
public class BatchPublishTimeLine implements Database.Batchable<sObject>{
sObjectIterable iterable;
Map<Id,User> userMap;
Map<String,String> contentmap;
public BatchPublishTimeLine(List<sObject> objectList,Map<String,String> contentmap){
iterable = new sObjectIterable(objectList);
this.userMap = new Map<Id,User>([Select Id,Name From User WHERE Authorize__c = true]);
this.contentmap = contentmap;
}
public Iterable<sObject> start(Database.BatchableContext BC){
return iterable;
}
public void execute(Database.BatchableContext BC, List<sObject> scope){
GMirrorUtil.createTimeLine(scope, contentMap);
}
public void finish(Database.BatchableContext BC){
System.debug('Job Has been Finished');
}
}
public static String doAPICall(String postBody, String method, String endPoint, String accessToken){
HttpRequest req = new HttpRequest();
Http http = new Http();
HttpResponse res;
req.setEndpoint(endPoint);
req.setMethod(method);
req.setHeader('Content-Type','application/json');
if(method == 'POST' || method == 'PUT')
req.setBody(postBody);
req.setHeader('Authorization','Bearer ' + accessToken);
res = http.send(req);
String result = res.getBody();
System.debug('status code is '+res.getStatus());
System.debug('result is'+res.getBody());
return result;
}
i am calling batch apex from a trigger after in sert function i am getting this error from line
String timelineRes = doApiCall(body,'POST','https://www.googleapis.com/mirror/v1/timeline',userSettings.AccessToken__c);
i am not using any DML statement after doApiCall function as you can see . but why i am facing trhis error ??Please guideline to resolve it.
The issue is that a DML operation was executed before the callout, not after. You have to run all database operations after the callout and that will fix this. I realize that this does not make any sense given the error, but I have seen this before and that is the solution.
can you please reply to this query i gave whole code in that in all of the code i didn't use even a single DML http://boards.developerforce.com/t5/Apex-Code-Development/Error-When-only-send-a-HttpCallout-in-batch-request/m-p/683333 only making a callout .please reply.