0

I am getting this error while running the batch. Any way to fix this. thanks

global class FileImpoLog 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;
            lstLogEntry.add(ML);
        }
        system.debug('lstLogEntry*******' + lstLogEntry);

        try{
            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);
            //lstBody.add(body);
        }

            system.debug('******body :' + body);
            String loginToken = MSOP_iManage_Service.getLoginToken();
            String targetUrl = 'https://test.com/api/v1/folders/folderName/documents';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(targetUrl);
            req.setHeader('x-auth-token', loginToken);
            req.setHeader('Content-Type', 'application/json');
            req.setMethod('POST'); 
            req.setBodyAsBlob(Body);  
            //req.setBody(Body);
            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){                     

    }
}
  • You cannot do a Callout after a DML. Do callout first and then DML – Pranay Jaiswal Mar 27 '19 at 10:47
  • Thanks Pranay, My req. is like I am getting all the tranaction for the day and inserting into log object and from there need make all the tranaction as blob and send it to other server thru rest API. in this case I have to insert here, can you suggest if I can achieve this other way? – Code_love Mar 27 '19 at 11:27
0

It appears to me that you need to refactor your code so you make the callout first to avoid this. I would recommend altering your code to something like the following and adding a custom exception class:

    try{
       sendLogEntry(list<File_Importer_Log_Entry__c>lstLogEntry);
       if(sendLogEntry = true){
           insert lstLogEntry(); 
       else throw new MyBatchDMLException('StatusCode not 200', e);
    }catch(CustomDmlException e){
        system.debug('***Error while inserting log entry - BatchName:FileImpoLog :' + e.getMessage());
    }


    boolean sendLogEntry(list<File_Importer_Log_Entry__c>lstLogEntry){
       boolean success = true;

       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 = 'https://test.com/api/v1/folders/folderName/documents';
          HttpRequest req = new HttpRequest();
          req.setEndpoint(targetUrl);
          req.setHeader('x-auth-token', loginToken);
          req.setHeader('Content-Type', 'application/json');
          req.setMethod('POST'); 
          req.setBodyAsBlob(Body);  
          Http http = new Http();
          HttpResponse res = http.send(req);
          system.debug('**********res : ' + res); 
          If(res.getStatusCode=200){
             objlogres.add('success');
          }else{
             objlogres.add(string.valueOf(res.getStatusCode));
             success = false;
          }
          return success;
       } 
    }

    public MyBatchDMLException extends Exception{}

EDIT:

In response to comments:

Most likely the reason your logs aren't being received at the server is because your batch class doesn't allow them. You'll want to append your class definition as follows:

global class FileImpoLog implements Database.Batchable<sobject>, Database.AllowsCallouts{
  • Hi , Since I had to send inserted record as blob so I used trigger inorder to insert the record on log object. after inserting the record into log object then I wrote batch class. Batch class is getting saved and running successfully(checked in apex job) however attachment is not receiving in external server. Here is my code – Code_love Mar 27 '19 at 18:39
  • Most likely you'll need to append your class declaration to use Database.AllowsCallouts. That should resolve your issue. I'll add that to my answer. – crmprogdev Mar 27 '19 at 19:05
  • I am not able to post my code here – Code_love Mar 27 '19 at 19:38
  • I am posting my code on here - developer.salesforce.com/forums/… – Code_love Mar 27 '19 at 19:51
  • You can also use the edit link to update your question here at any time. It will automatically go into the queue to be voted on for re-opening. I see no reason why yours wouldn't. – crmprogdev Mar 27 '19 at 23:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.