I'm getting this error, System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out. After executing the class. Please Help in this to resolve this issue.
Apex class:
public with sharing class CalloutService {
private static final string url='https://th-apex-http-callout.herokuapp.com/animals';
@future(callout=true)
public static void runCalloutServices(){
List<Account> account=new List<Account>();
List<Task> taskList = new List<Task>();
account=[Select id, Name From Account where Name='Greenfield'];
for(Integer i=0; i<100;i++){
HttpRequest req=new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
Http http=new Http();
HttpResponse res=new HttpResponse();
res=http.send(req);
System.debug(res);
String strRes='';
strRes=res.getBody();
System.debug('*****Response'+ res.getStatusCode());
if(res.getStatusCode() == 200){
System.debug('Status Code:'+res.getStatusCode());
Task task = new Task(
Subject='Josh',
Status='In Progress',
Priority='Normal',
WhatId=account[0].Id);
System.debug(task);
insert task;
Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
mail.setToAddresses(new string[] {'pallaviyemle15@gmail.com'});
mail.setSubject('A new Hope');
mail.setHtmlBody('Callout');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );
}
}
}
}
Thanks,
Pallavi
3 respostas
Hi,
Still I'm getting this error, System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
Thanks,
Pallavi