+ Start a Discussion
arabisaysarabisays 

Problem with testing Batch Job

I am trying to get test coverage for the following batch job
global class ApprovalMailBatchJob implements Database.Batchable<sObject>
{
  public String QuerySalesTeam;
  public Map<Id,String> User_EmailMap =new Map<Id,String>();
  public Map<Id,String> User_NameMap =new Map<Id,String>();
    global Database.QueryLocator start(Database.Batchablecontext BC)
    {
     QuerySalesTeam='select Id,Approver__c,Approver__r.Email,Approver__r.FirstName from Contact_Team__c where Status__c=\'Pending\' ORDER BY Approver__c';
     System.debug(QuerySalesTeam);
     return Database.getQueryLocator(QuerySalesTeam);
      
    }
   
    global void execute(Database.BatchableContext BC,List<SObject> scope)
    {
     List<Contact_Team__c> conList=new List<Contact_Team__c>();
     conList=(List<Contact_Team__c>)scope;
     System.debug(conList);
     for(Contact_Team__c conTeam:conList)
     {
        if(!User_EmailMap.containskey(conTeam.Approver__c))
        {
            User_EmailMap.put(conTeam.Approver__c,conTeam.Approver__r.Email);
            User_NameMap.put(conTeam.Approver__c,conTeam.Approver__r.FirstName);
        }
                 
     }
      System.debug(User_EmailMap);
    System.debug(User_NameMap);

    }
   
    global void finish(Database.BatchableContext BC)
    {
    System.debug('UserMap'+User_EmailMap);
    System.debug('User Name Map'+User_NameMap);

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        String[] toAddresses ;
        for(Id usr:User_EmailMap.keyset())
        {
         
          Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
        
        
     
         // Assign the addresses for the To and CC lists to the mail object.
          mail.setTargetObjectId(usr);
         // Specify the name used as the display name.
          mail.setSenderDisplayName(UserInfo.getName());
         // Specify the subject line for your email address.
          mail.setSubject('Needed Pending Approval Request in Salesforce.com' );
         
          String str_HtmlBody='';
          str_HtmlBody=str_HtmlBody + '<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><HTML><HEAD></HEAD><BODY>';
          str_HtmlBody=str_HtmlBody + '       <H5>Dear ' + User_NameMap.get(usr)+ '</H5><P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your approval is required for the requested staff code change(s). ';
          str_HtmlBody=str_HtmlBody + '       <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Please visit the link to view details. ';

          str_HtmlBody=str_HtmlBody + '           <A HREF=\"https://cs10.salesforce.com/home/home.jsp\">Click here </A>';
          str_HtmlBody=str_HtmlBody + '       <P>Regards,<P>' + UserInfo.getName() + '</BODY></HTML>';                                               
         
          mail.setHtmlBody(str_HtmlBody);
        
         // Send the email you have created.
          mails.add(mail);
         }
         if(mails.size()>0)
         {
           Messaging.sendEmail(mails);
         }
        }

}

While running the test , the Maps (highlighted in bold) not getting passed from execute to finish method. 
Here is the log.

[32]|DEBUG|{null=null, 005J0000001LT3eIAG=test.user@hexgn.com
[33]|DEBUG|{null=null, 005J0000001LT3eIAG=Test User
[38]|DEBUG|UserMap{}
[39]|DEBUG|User Name Map{}

why it is happening?
Subhash GarhwalSubhash Garhwal
Hi,
I think you need to modify your batch Like:
global class ApprovalMailBatchJob implements Database.Batchable<sObject>, Database.Stateful 
and also make QuerySalesTeam, User_EmailMap,User_NameMap variables global

Thanks


Praveen JhaPraveen Jha
t