function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DJ 367DJ 367 

First error: You have uncommitted work pending. Please commit or rollback before calling out

Hello All,

Any way to fix this. Thanks
global class TEstLog implements Database.Batchable<sobject> {   
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select Id,Document_Process_Date__c,Workspace__c,CreatedBy.Name,Folder_Name__c,CreatedDate,CreatedById from FileImporter__c where  CreatedDate = today and Doc_Status__c != null');


    global void execute(Database.BatchableContext BC, list<Sobject> obj){  
        List<File_Importer_Log_Entry__c> lstLogEntry = new List<File_Importer_Log_Entry__c>();
        system.debug('lstFileImpor******' + obj); 
        for(Sobject o : obj){
            FileImporter__c  M = (FileImporter__c ) o;
            File_Importer_Log_Entry__c ML = new File_Importer_Log_Entry__c();
            ML.created_by__c = M.CreatedBy.Name;
            ML.Document_Process_Date__c = M.Document_Process_Date__c;
            ML.WorkspaceId__c = M.Workspace__c;
            ML.Workspace_Name__c = M.Folder_Name__c; 
            ML.Created_Date__c = M.CreatedDate;
        system.debug('lstLogEntry*******' + lstLogEntry);
            insert lstLogEntry;
        //List<blob> lstBody;
        Blob body;
        for(File_Importer_Log_Entry__c objLog : lstLogEntry){
             body = Blob.valueOf('Created By:'+objLog.created_by__c+'/n'+'Document Process Date :'+objLog.Document_Process_Date__c+'/n'+'WorkspaceId :'+objLog.WorkspaceId__c+'/n'+'Workspace Name'+objLog.Workspace_Name__c+'/n'+'Created Date :'+objLog.Created_Date__c);
			system.debug('******body :' + body);
			String loginToken = MSOP_iManage_Service.getLoginToken();
			String targetUrl = '';
			HttpRequest req = new HttpRequest();
			req.setHeader('x-auth-token', loginToken);
			req.setHeader('Content-Type', 'application/json');
			Http http = new Http();
			HttpResponse res = http.send(req);
			system.debug('**********res : ' + res);  
        }catch(DmlException e){
            system.debug('***Error while inserting log entry - BatchName:FileImpoLog :' + e.getMessage());
    global void finish(Database.BatchableContext BC){                     


Best Answer chosen by DJ 367
DJ 367DJ 367
I have inserted record thru trigger and after queried value in batch in it worked.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 

Greetings to you!

The issue occurs when you first perform DML and thn call out in the same transaction. Please refer to below Salesforce Knowledge Article for mor information.

All the DML operations should be invoked only after you are done with callouts. So, make a callout first and then save the request.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
DJ 367DJ 367
Hi Khan,

Thanks for reply, Yes already I have gone thru this but not able to fix this, it is possible if you would modify the code.
DJ 367DJ 367
I have inserted record thru trigger and after queried value in batch in it worked.
This was selected as the best answer