I have a little problem with my batch:
List<String> idList; public My_Batch(List<String> ocIdList) { idList= ocIdList; } global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator('Select Id from Account where Id IN : idList '); } global void execute(Database.BatchableContext BC, List<Account> records) { for(Account acc : records){ String endpoint = 'https://urlendpoint/v1/'; Http http = new Http(); HttpRequest request = new HttpRequest(); request.setHeader('Authorization', 'xxxxxxx'); request.setEndpoint(endpoint); request.setMethod('GET'); HttpResponse response = http.send(request); CustomLog log = new CustomLog(); log.Status__c = 'Success'; insert log; acc.log__c = TRUE; update acc; } } This batch is called from the following INVOCABLE method:
//.......staff......// CustomLog log = new CustomLog(); log.Status__c = 'Success'; insert log; if(idList.size()>0){ Database.executeBatch(new My_Batch(idList), 1); } Most of the batch executions are successful but few of them return the following error : "You have uncommitted work pending. Please commit or rollback before calling out."
I know that the issue occurs when you first perform DML and then callout in the same transaction.
I don't understand two things:
1) In the execute of the batch I have first the callout and then the DML, so I shouldn't get any error.
2) Most of the executions are successful but few of them return the below error message. Why do they have different result?
Thanks in advance