You need to sign in to do that
Don't have an account?
Sravana Sudheer
System. Dml exception: You have uncommitted work pending,please commit before calling out
Hello All,
I am doing an api callout to an 3rd party website and i am writing an schedule class for that. while executing that scheduled calss i am getting the above error saying that you have some uncommitted work pending,please commit before rolling out. I have googled a lot about this and seems like we cannot callout after performing an Dml operations,
But in my code i am not able to seperate Dml and callout. Even i added @future annotation before the method which i used for Callout. It is retrieving the details of the first record after that when going to the second callout it is giving that error.
Basically it is following the unstructured format like doing a callout first and doing DML after that and again going for the callout- Which seems like not a best practice. So any suggestions in code will be a huge help.
I am doing an api callout to an 3rd party website and i am writing an schedule class for that. while executing that scheduled calss i am getting the above error saying that you have some uncommitted work pending,please commit before rolling out. I have googled a lot about this and seems like we cannot callout after performing an Dml operations,
But in my code i am not able to seperate Dml and callout. Even i added @future annotation before the method which i used for Callout. It is retrieving the details of the first record after that when going to the second callout it is giving that error.
Basically it is following the unstructured format like doing a callout first and doing DML after that and again going for the callout- Which seems like not a best practice. So any suggestions in code will be a huge help.
@future(callout=true) public static void processAllOpenRequisition(){ bulkify = true; u = [SELECT Username_Broadbean__c,Password_Broadbean__c From USER WHERE id =:UserInfo.getUserId()]; HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setEndpoint('https://api.adcourier.com/hybrid/hybrid.cgi'); req.setHeader('Content-Type', 'text/xml'); requisitionRecMap = new Map<id, Recruiting_Activities__c>([SELECT Time_now__c,Time_From__c FROM Recruiting_Activities__c WHERE EL_Status__c <> 'Closed' AND REcordtype.Name=:'Requisition' ]); system.debug('hellooooo'+requisitionRecMap); readObject(requisitionRecMap.keySet()); --> Here calling the another method for XML req.setBody(xmlData); Http http = new Http(); req.setTimeout(60000); if(!test.isrunningtest()){ HttpResponse res = http.send(req); resbody = res.getBody(); System.debug('*****'+res.getBody()); // Here you will get the response } parseTest(); --> It is the method for parsing the data, here we are doing DML operations. } public static void readObject(Set<Id> recids){ for(Recruiting_Activities__c ra:[SELECT Time_now__c,Time_From__c,Req_ID__c FROM Recruiting_Activities__c where Id IN:recids]){ JobReference=ra.Req_ID__c; xmlData+='<?xml version="1.0" encoding="utf-8" ?>' +'<AdCourierAPI>' +'<Method>RetrieveApplications</Method>' +'<APIKey>3012861772</APIKey>' +'<Account>' +'<UserName>'+u.Username_Broadbean__c+'</UserName>' +'<Password>'+u.Password_Broadbean__c+'</Password>' +'</Account>' +'<Options>' +'<Filter>' +'<JobReference>'+JobReference+'</JobReference>' +'<Times>' +'<TimeFrom>ra.Time_From__c</TimeFrom>' +'<TimeTo>2020-12-24T06:15:00Z</TimeTo>' +'</Times>' +'<Id></Id>' +'<Rank>All</Rank>' +'</Filter>' +'<BodyFormat EncodingType="None" />' +'<EmbedDocuments EncodingType="Base64">true</EmbedDocuments>' +'<XMLFormat EncodingType="Base64">BGT</XMLFormat>' +'<IncludeAddress>false</IncludeAddress>' +'</Options>' +'</AdCourierAPI>'; system.debug('--------'+xmlData); } }
Please share the triggering code that is executing this future method.
Thanks
AR
Actually we are executing the logic from processAllOpenRequisition() method. And Parsetest() is the method which we are using to parse the response and inserting the DMLS. So basically what am i doing is i am just calling processAllOpenRequisition() method from schedule class and by checking the debugs it is giving me this error.
Thank you,
Sravan
And are you getting this error while testing or live integration?
@Anupam Rastogi
sure. While doing callout i am getting this error in my sandbox.it will go live in next Month.
can you please help me with this issue and let me know where i am doing wrong!
@goabhigo
HEllo Abhi can you share your experience on how your issue gets resolved!
Thanks,
Sravan
How did you resolve the issue?
I completely took off DML's in the callout by adding values to the list and then performing DML in the parsetest method in my code above.
Here is my code.. i am totally new to apex.. can you tell me what should i do