I have a class apex that call web service REST and SOAP. This class apex first call the API REST and process de data, when get all data, this data sent to API SOAP.
The class function correctly, but when a try to validate the class apex in set changes.
I HAVE THIS ERROR:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
THIS IS THE CLASS APEX
public static PackageActivationAllEmp.ArrActivateAll_element[] getArrActivateAllRelojesCob(ORD_WrapperActivacion wrapper, String idCotPlanServicio){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(EndPoint__c.getValues('infoCount').URL__c);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setHeader('Accept', 'application/json');
request.setTimeout(120000);
String enviojson = '{"Login" : {"userId" : "26351", "password" : "Middle100$", "ip" : "127.2.1.0"}, "AcountNo" : "' + cuentaFact + '", "RFC" : "' + rfc + '"}';
request.setBody(enviojson);
HttpResponse response;
if (Test.isRunningTest()){
Test.setMock(HttpCalloutMock.class, new InfoRelojMock());
response = http.send(request);
}else{
response = http.send(request);
}
if (response.getStatusCode() == 200){
datos = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
resultWSRelojEstatus = String.valueOf(datos.get('ResultDes'));
Map<String, Object> dim = (Map<String, Object>)datos.get('AcountDesc');
datos.put('reloj', dim.get('CollectorId'));
resultWSReloj = String.valueOf(dim.get('CollectorId'));
resultWSSupresion = String.valueOf(dim.get('Collections'));
}else{
datos.put('response', response);
}
}
THIS IS THE CLASS TEST
@isTest static void getAActiva(){
Cot_SitioPlan__c cspCtaFact = [SELECT Id, Name FROM Cot_SitioPlan__c LIMIT 1];
ORD_WrapperActivacion wrapper = ORD_Util_Activacion.getWrapperActivacion(cspCtaFact.Id);
Test.startTest();
Test.setMock(HttpCalloutMock.class, new InfoRelojMock());
ActivacionServiciosControllerUniversal.getArrActivateAllRelojesCob(wrapper, cspCtaFact.Id);
Test.stopTest();
}
THIS IS CLASS MOCK
global class InfoRelojMock implements HttpCalloutMock{
global HttpResponse respond (HttpRequest request){
HttpResponse response = new HttpResponse();
response.setHeader('Content-Type', 'application/json');
response.setBody('{"Result":{"IdResult": "600464","Result":"0","ResultDes":"Successful Operation","AcountDesc": {"CollectorId":"5","Collections":"0","StatusSupresionPermanente":[{"AUTHORIZATION_ID":null,"VALUE": "0","CREATED_T": "2002-04-20T10:21:05"},{"AUTHORIZATION_ID": "MAX FER GUSON","VALUE": "1","CREATED_T": "2002-07-16T17:37:00"}]}}}');
response.setStatusCode(200);
return response;
}
}
HOW TO SOLVE THE System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out ?
Hi Cristiano,
Have you looked at the stack trace to verify it's actually this code that's causing the problem? It could be something in the test setup, or possibly the generation of the wrapper that's crashing.