‘Salesforce Batch Apex’

Sending more than 10 E-mails from Apex:

------------------------------------------------------------
global class Batch_CaseEmail implements Database.Batchable<sObject>,Database.Stateful
{
   Map<Id , List<Case>> userCaseMap {get; set;}
   List<Case> allCaseLoggedToday {get; set;}

   global Batch_CaseEmail()
   {
       //Map to maintain user id and cases logged by them today
       userCaseMap = new Map<Id, List<Case>>() ;

       //All sales rep (System admins)
       List<User> salesRep = new List<User>() ;
       salesRep = [select id , name , Email , ManagerId from User
                    where Profile.Name = 'System Administrator'] ;

       //All sales rep ids
       List<Id> salesIds = new List<Id>() ;
       for(User ur : salesRep)
       {
           salesIds.add(ur.Id) ;
       }

       //All cases logged today by sales rep
       allCaseLoggedToday = new List<Case>() ;
       allCaseLoggedToday = [select Id, CaseNumber,CreatedById, Owner.name 
                            , account.Name , contact.name from Case 
                            where CreatedDate = TODAY AND CreatedById in : salesIds] ;
   }

   global Database.QueryLocator start(Database.BatchableContext BC)
   {
      //Creating map of user id with cases logged today by them
      for(Case c : allCaseLoggedToday)
      {
          if(userCaseMap.containsKey(c.CreatedById))
          {
              //Fetch the list of case and add the new case in it
              List<Case> tempList = userCaseMap.get(c.CreatedById) ;
              tempList.add(c);
              //Putting the refreshed case list in map
              userCaseMap.put(c.CreatedById , tempList) ;
          }
          else
          {
              //Creating a list of case and outting it in map
              userCaseMap.put(c.CreatedById , new List<Case>{c}) ;
          }
      }

      //Batch on all system admins (sales rep)
      String query = 'select id , name , Email from User 
                     where Profile.Name = \'System Administrator\'';
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope)
   {

      for(Sobject s : scope)
      {
          //Type cast sObject in user object
          User ur = (User)s ;

          //If system admin has logged any case today then only mail will be sent
          if(userCaseMap.containsKey(ur.Id))
          {
              //Fetching all cases logged by sys admin
              List<Case> allCasesOfSalesRep = userCaseMap.get(ur.Id) ;

              String body = '' ;
              //Creating tabular format for the case details
              body = BodyFormat(allCasesOfSalesRep) ;

              //Sending Mail
              Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;

              //Setting user email in to address
              String[] toAddresses = new String[] {ur.Email} ;

              // Assign the addresses for the To and CC lists to the mail object
              mail.setToAddresses(toAddresses) ;

              //Email subject to be changed
              mail.setSubject('New Case Logged');

              //Body of email
              mail.setHtmlBody('Hi ' + ur.Name + ',

 Details of Cases logged today is as follows : 

' + body + '
 Thanks');

              //Sending the email
              Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
          }
      }
   }

   public String BodyFormat(List<Case> lst)
   {
       String str = '' ;
       for(Case cs : lst)
       {
           str += '<tr><td>'+ cs.CaseNumber +'</td>'+'<td>'+ cs.Owner.Name 
                   +'</td>'+'<td>'+ cs.Account.Name +'</td>'
                   +'<td>'+ cs.Contact.Name +'</td>'+'</tr>' ;
       }
       str = str.replace('null' , '') ;
       String finalStr = '' ;
       finalStr = '<table border="1"> <td> CaseNumber </td> <td> Owner </td> 
                   <td> Account </td> <td> Contact </td> ' + str +'</table>' ;
       return finalStr ;
   }

   global void finish(Database.BatchableContext BC)
   {
   }

}

Execute this class using below script in system logs :
Batch_CaseEmail controller = new Batch_CaseEmail() ;
Integer batchSize = 1;
database.executebatch(controller , batchSize);


Output :

Hi xyz , Details of Cases logged today is as follows :
CaseNumberOwnerAccountContact
00001031Ramaxyz2day check sds

IMP Notes:

1) To monitor or stop the execution of the batch Apex job, from Setup, click Monitoring | Apex Jobs or Jobs | Apex Jobs.
2) a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an organization.
3)Default batchSize  is 200.
4)The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.

FOr More Info:


http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

Comments

Popular posts from this blog

Approval Process

Spring Batch 2.0 -Basic Concepts

Sending Mass Emails From Salesforce