+ Start a Discussion
ishchopraishchopra 

Error on Job Run

Hello Everyone,

this is what i am getting when job runs:

First error: You have uncommitted work pending. Please commit or rollback before calling out​
 
global class EmailReports implements Schedulable 

{
        global void execute(SchedulableContext sc)
        {
               CallSchedule.EmailReports();
              
       }

   }
 
public class CallSchedule {
            
        @future(callout=true)
        public static void EmailReports()
        {

        String ReportID;
        String EvntName;
        
        List<string> reportids = new List<string>{'00OM0000000GzioMAC'};  // reports to send
        List<string> EventName = new List<string>{'EAGC+2016','LNGCAN+2017','CEE+2017','GASASIA+2016','Gastech+2017','GIS+2017'};  // Event Value to be passed runtime 
          
       
       
        if (EventName.size() >0)
        {
        
        for (integer i=0 ; i < 5 ; i++)
        {
        
        ReportID = reportids[0];
        EvntName = EventName[i]; 
          
                           
        ApexPages.PageReference report = new ApexPages.PageReference('/'+ ReportID + '?pv0=' + EvntName +'&csv=1');
              
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        //attachment.setBody(Blob.valueof(report1.getContent().toString()));
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report For' + EventName );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'skh@gmail.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
       
        }
        }   
     }

 
Best Answer chosen by ishchopra
ishchopraishchopra
here is the solution folks
 
public class CallSchedule {
            
        @future(callout=true)
        public static void EmailReports()
        {

        String ReportID;
        String EvntName;
        
        List<string> reportids = new List<string>{'00OM0000000GzioMAC'};  // reports to send
        List<string> EventName = new List<string>{'EAGC+2015','EAGC+2016','GASTECH+2015'};  // Event Value to be passed runtime 

        List<Messaging.EmailFileAttachment> attachment = new List<Messaging.EmailFileAttachment>(); 

       Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
       
        if (EventName.size() >0)
        {
        
        for (integer i=0 ; i < 3 ; i++)
        {
        
        ReportID = reportids[0];
        EvntName = EventName[i]; 
          
                           
        ApexPages.PageReference report = new ApexPages.PageReference('/'+ ReportID + '?pv0=' + EvntName +'&csv=1');
              
        
        
        
        //For(integer a=0;a=<i;a++)
       // {
        
        Messaging.EmailFileAttachment attmnt = new Messaging.EmailFileAttachment();
        
         attmnt.setFileName(EvntName);
         blob b = report.getContent();
         
         attmnt.setbody(b);
       
        //EncodingUtil.base64Encode(report.getContent().toString());
        //attmnt.setBody(blob.valueof(EncodingUtil.base64Encode(report.getContent())));
        //attmnt.setBody(blob.valueof(report.getContent().toString()));
       
          attmnt.setContentType('text/csv;charset=UTF-8');
          attachment.add(attmnt);
          // system.debug(attachment[i]);
        // }       
       
       // message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report For' + EventName );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'ishchopra@gmail.com' } );
        message.setFileAttachments(attachment);             
         
        }
        }   
    
    
    Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
     }}

 

All Answers

Dilip_VDilip_V
Hi Chopra,

It seems there are some pendig jobs.
Go to
Monitoring->Scheduled jobs
Delete them and save.

Let me know if it helps.

mark it as best answer if it helps.

Thanks.
ishchopraishchopra
i am doing the test in Sandbox and we dont have any active job
Dilip_VDilip_V
Chopra,

We can not make a DML operation or send email after after you make callout.

So follow this order.
Callout 
Callout
DML/Send Email

Please take a look at this.
https://help.salesforce.com/apex/HTViewSolution?id=000079772&language=en_US

Let me know if it helps.

mark it as best answer if it helps.

Thanks.
ishchopraishchopra
Hey Thermo,

Appreciate your help on this, is there any way you can tell me where to place the Callout in my code above?

i am really new to apex therefore couldn't figure this out

A
Dilip_VDilip_V
Chopra,

That is what I am wondering at what line you are doing callout?
Please let me know.

Give it a try once.
public class CallSchedule {
            
        @future
        public static void EmailReports()
        {

        String ReportID;
        String EvntName;
        
        List<string> reportids = new List<string>{'00OM0000000GzioMAC'};  // reports to send
        List<string> EventName = new List<string>{'EAGC+2016','LNGCAN+2017','CEE+2017','GASASIA+2016','Gastech+2017','GIS+2017'};  // Event Value to be passed runtime 
          
       
       
        if (EventName.size() >0)
        {
        
        for (integer i=0 ; i < 5 ; i++)
        {
        
        ReportID = reportids[0];
        EvntName = EventName[i]; 
          
                           
        ApexPages.PageReference report = new ApexPages.PageReference('/'+ ReportID + '?pv0=' + EvntName +'&csv=1');
              
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        //attachment.setBody(Blob.valueof(report1.getContent().toString()));
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report For' + EventName );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'skh@gmail.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
       
        }
        }   
     }

mark it as best answer if it helps.

Thanks.
ishchopraishchopra
I tried that code and got this Error

First error: Callout not allowed from this future method. Please enable callout by annotating the future method. eg: @Future(callout=true)
Dilip_VDilip_V
Chopra,
At what line you are doing callout?Please let me know
ishchopraishchopra
This is the code i have used
 
public class CallSchedule {
            
        @future
        public static void EmailReports()
        {

        String ReportID;
        String EvntName;
        
        List<string> reportids = new List<string>{'00OM0000000GzioMAC'};  // reports to send
        List<string> EventName = new List<string>{'EAGC+2016','LNGCAN+2017','CEE+2017','GASASIA+2016','Gastech+2017','GIS+2017'};  // Event Value to be passed runtime 
          
       
       
        if (EventName.size() >0)
        {
        
        for (integer i=0 ; i < 5 ; i++)
        {
        
        ReportID = reportids[0];
        EvntName = EventName[i]; 
          
                           
        ApexPages.PageReference report = new ApexPages.PageReference('/'+ ReportID + '?pv0=' + EvntName +'&csv=1');
              
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(Blob.valueof(report.getContent().toString()));
        //attachment.setBody(Blob.valueof(report1.getContent().toString()));
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report For' + EventName );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'ishchopra@gmail.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
       
        }
        }   
     } 
     
      

    
    }

 
ishchopraishchopra
Please help m completely stuck
ishchopraishchopra
here is the solution folks
 
public class CallSchedule {
            
        @future(callout=true)
        public static void EmailReports()
        {

        String ReportID;
        String EvntName;
        
        List<string> reportids = new List<string>{'00OM0000000GzioMAC'};  // reports to send
        List<string> EventName = new List<string>{'EAGC+2015','EAGC+2016','GASTECH+2015'};  // Event Value to be passed runtime 

        List<Messaging.EmailFileAttachment> attachment = new List<Messaging.EmailFileAttachment>(); 

       Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
       
        if (EventName.size() >0)
        {
        
        for (integer i=0 ; i < 3 ; i++)
        {
        
        ReportID = reportids[0];
        EvntName = EventName[i]; 
          
                           
        ApexPages.PageReference report = new ApexPages.PageReference('/'+ ReportID + '?pv0=' + EvntName +'&csv=1');
              
        
        
        
        //For(integer a=0;a=<i;a++)
       // {
        
        Messaging.EmailFileAttachment attmnt = new Messaging.EmailFileAttachment();
        
         attmnt.setFileName(EvntName);
         blob b = report.getContent();
         
         attmnt.setbody(b);
       
        //EncodingUtil.base64Encode(report.getContent().toString());
        //attmnt.setBody(blob.valueof(EncodingUtil.base64Encode(report.getContent())));
        //attmnt.setBody(blob.valueof(report.getContent().toString()));
       
          attmnt.setContentType('text/csv;charset=UTF-8');
          attachment.add(attmnt);
          // system.debug(attachment[i]);
        // }       
       
       // message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report For' + EventName );
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'ishchopra@gmail.com' } );
        message.setFileAttachments(attachment);             
         
        }
        }   
    
    
    Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
     }}

 
This was selected as the best answer