Below class gives error "You have uncommitted work pending. Please commit or rollback after calling out".
//Call out from batch
public void execute(Database.BatchableContext bc, List<Opportunity> scope) {
for(Opportunity o : scope){
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint('https://posttestserver.com/post.php/?dump&dir=sandbox&' +o.name);
req.setHeader('Content-Type','x-www-form-urlencoded');
req.setHeader('Content-Length', '0');
system.debug('here5');
req.setMethod('GET');
req.setTimeOut(1200);
res = http.send(req);
String str = res.toString();
}
//Test Method
@isTest
static void testRecordUpdate(){
User u = createTestUser('System Administrator', 'Test', 'User');
List<Opportunity> opp = createOppRecord('Open', 1, u);
for(Opportunity o : opp){
o.stageName = 'Closed Won';
}
Test.setMock(HttpCalloutMock.class, new CalloutMock());
update opp; // this update makes callout
}
Trying to : When agent updates an opportunity, a callout is made and if response is successful, a record should be created in child object
Thanks.