+ Start a Discussion
penchala rajupenchala raju 

can i send a data through mail from finish() method whatever i fetched from batch apex execute method.can any only .please see the below code

global class batchupdate implements Database.Batchable<sobject>
{
   // list<account> s{set;get;}
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String Query =  'select id,Transaction_type__c,amount_info__c from Transaction__c';
        System.debug('------Query---->'+Query);
        return Database.getQueryLocator(Query);
    }
    global void execute(Database.BatchableContext bc,list<Transaction__c> scope)
    {
     // s=new list<account>();
        list<Transaction__c> cust=new List<Transaction__c>();
        System.debug('------scope---->'+scope.size());
       // scope=s;
        for(Transaction__c c:scope)
        {
            c.amount_info__c=5656;
            cust.add(c);
           //  system.debug(cust);
        }

        update cust;
       
    }
    global void finish(Database.BatchableContext bc)
{
    Messaging.SingleEmailMessage mail1=new Messaging.SingleEmailMessage();
            String[] toadd=new String[]{'praju.salesforce@gmail.com'};
           mail1.setToAddresses(toadd);
            mail1.setSubject('Test mail');
            mail1.setPlainTextBody('this is a test mail');
                        Messaging.SingleEmailMessage mail2=new Messaging.SingleEmailMessage();
            mail1.setToAddresses(toadd);
            mail1.setSubject('Test mail');
            mail1.setPlainTextBody(list<Transaction__c> cust);
            Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail1});
    }

     
}
Himanshu ParasharHimanshu Parashar
Hi Raju,

Yes you can send that data. what is the issue you are facing in that.


Thanks,
Himanshu
penchala rajupenchala raju
i want to send the data.but i have to know how to send means where we share the data in email sending
 
Himanshu ParasharHimanshu Parashar
Hi Raju,

Please check following code
global class batchupdate implements Database.Batchable<sobject>,Database.Stateful
{
    global List<Transaction__c> Trans_List;
   // list<account> s{set;get;}
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        String Query =  'select id,Transaction_type__c,amount_info__c from Transaction__c';
        System.debug('------Query---->'+Query);
        return Database.getQueryLocator(Query);
    }
    global void execute(Database.BatchableContext bc,list<Transaction__c> scope)
    {
     // s=new list<account>();
        list<Transaction__c> cust=new List<Transaction__c>();
        System.debug('------scope---->'+scope.size());
       // scope=s;
        for(Transaction__c c:scope)
        {
            c.amount_info__c=5656;
            cust.add(c);
           //  system.debug(cust);
           Trans_List.add(c);
        }

        update cust;
       
    }
    global void finish(Database.BatchableContext bc)
{
      //create body for email

      String BodyText = '';

      for(Transection__c ts : Trans_List)
      {
            BodyText += '***********************************************'+'\n';   
            BodyText += '***********************************************'+'\n';
            BodyText += YOUR DETAILS FROM CURRENT RECORD; //ts.FIELDNAME__C              

       }

    Messaging.SingleEmailMessage mail1=new Messaging.SingleEmailMessage();
            String[] toadd=new String[]{'praju.salesforce@gmail.com'};
           mail1.setToAddresses(toadd);
            mail1.setSubject('Test mail');
            mail1.setPlainTextBody('this is a test mail');
                        Messaging.SingleEmailMessage mail2=new Messaging.SingleEmailMessage();
            mail1.setToAddresses(toadd);
            mail1.setSubject('Test mail');
            mail1.setHtmlBody(BodyText);
            Messaging.sendEmail(new Messaging.singleEmailMessage[]{mail1});
    }

     
}

there are two things which I have added. 

1. Database.Stateful interface at the top which will maintain state throughout the execution
2. global Trans_List which can be used to retain record so that we can get processed record in finish method.


Let me know if you have any question

Thanks,
Himanshu