function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suraj DemgundeSuraj Demgunde 

facing problem where writing test class for below class the problem is org is relation with wcb and email field indirectly referncing wcb object

class;

global class ICC_ReminderEmails implements Database.Batchable<sObject>{
    
    
     global list<ICCWorkbench__c> start(Database.BatchableContext BC){
        return [select id,name,Organization_ID__c,Vendor_Name__r.name,Vendor_Name__r.Email__c,Vendor_Name__r.Email1__c,Vendor_Name__r.Email2__c,Vendor_Name__r.Group_Email__c,MppsubmittDate__c,CreatedDate from ICCWorkbench__c where Supplier_Status__c ='Submitted To Supplier' AND MppsubmittDate__c=Last_n_days:1];
   }

     global void execute(Database.BatchableContext BC, List<ICCWorkbench__c> scope){
        system.debug('Scope -->' + scope);
        List < Messaging.SingleEmailMessage > emails = new List < Messaging.SingleEmailMessage > ();
           
        for(ICCWorkbench__c wcb : scope){
                
                               List<String> emailAddress  = wcb.Vendor_Name__r.Group_Email__c.split(';'); 
                               System.debug(emailAddress);
                               if(wcb.Vendor_Name__r.Email__c != null)
                                emailAddress.add(wcb.Vendor_Name__r.Email__c);
                               if(wcb.Vendor_Name__r.Email1__c != null)
                                emailAddress.add(wcb.Vendor_Name__r.Email1__c);
                               if(wcb.Vendor_Name__r.Email2__c != null)
                                emailAddress.add(wcb.Vendor_Name__r.Email2__c);
                               Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                               email.setToAddresses(emailAddress);
                               email.setSubject('Reminder For Pending Po Acknowledge '); 
                               email.setPlainTextBody('Dear User,' +' '+
                                                                'Your PO Number '+' '+wcb.Name +' ' + 'Pending For Acknowledge if Already Acknowledge then Ignore  this Mail otherwise Submit it immedietly.');
                                                      
                               emails.add(email);
                               system.debug('List Of Emails -->' + emails);
            
           
          }
       Messaging.sendEmail(emails);
      }
    
    global void finish(Database.BatchableContext BC){
   }
}


test class

@isTest
Public class TestICC_ReminderEmails {  
public static testMethod void TestRemindermails(){   

LisT<ICCWorkbench__c>  query = [select id,name,Vendor_Name__c ,Organization_ID__c,Vendor_Name__r.name,Vendor_Name__r.Email__c,Vendor_Name__r.Email1__c,Vendor_Name__r.Email2__c,Vendor_Name__r.Group_Email__c,MppsubmittDate__c,CreatedDate from ICCWorkbench__c where Supplier_Status__c ='Submitted To Supplier' AND MppsubmittDate__c=Last_n_days:1];
RecordType rtvnd = [select id,Name from RecordType where Name='Vender'];
ICCOrganization__c org = new ICCOrganization__c();
org.RecordTypeID = rtvnd.id;
org.Name = 'test3';
org.Email__c = 'surajdem@gmail.com';     
org.Email1__c='surajdem@gmail.com';
org.Email2__c = 'harshard.pansare@zamilindustrial.com'; 
org.Group_Email__c='surajdem@gmail.com;harshad.pansare@zamilindustrial.com';
insert org;
System.debug(org);

RecordType rtvnd1 = [select id,Name from RecordType where Name ='Manual FPO'];    
ICCWorkbench__c wcb = new ICCWorkbench__c();
wcb.RecordTypeID = rtvnd1.id;
wcb.Vendor_Name__c=org.id;
wcb.Name='hchchchchchc2';
wcb.MppsubmittDate__c= Date.newInstance(2014,1,21);
wcb.Organization_ID__c= '82';
wcb.Supplier_Status__c = 'Submitted To Supplier';
//wcb.Vendor_Name__r.Email__c  = org.Email__c;
//wcb.Vendor_Name__r.Email1__c= org.Email1__c;
//wcb.Vendor_Name__r.Email2__c = org.Email2__c;
//wcb.Vendor_Name__r.Group_Email__c=org.Group_Email__c;

//wcb.CurrencyIsoCode = 'EUR-Euro';

insert wcb;
System.debug(wcb);

test.startTest();  

//initiating an instance of the batch job   
ICC_ReminderEmails b = new ICC_ReminderEmails();
Database.executeBatch(b);

test.stopTest();  


}
}
Raj VakatiRaj Vakati
Try like this
 
@isTest
Public class TestICC_ReminderEmails {  
public static testMethod void TestRemindermails(){   


for(Integer i = 0 ; i<10 ; i++){
ICCWorkbench__c wc = new ICCWorkbench__c()l 
wc.Supplier_Status__c ='Submitted To Supplier' ; 
wc.MppsubmittDate__c=System.today()-1 ; 
insert wc ; 
}


Id RecordTypeIdContact = Schema.SObjectType.ICCOrganization__c.getRecordTypeInfosByName().get('Vender').getRecordTypeId();




ICCOrganization__c org = new ICCOrganization__c();
org.RecordTypeID = RecordTypeIdContact;
org.Name = 'test3';
org.Email__c = 'surajdem@gmail.com';     
org.Email1__c='surajdem@gmail.com';
org.Email2__c = 'harshard.pansare@zamilindustrial.com'; 
org.Group_Email__c='surajdem@gmail.com;harshad.pansare@zamilindustrial.com';
insert org;
System.debug(org);

RecordType rtvnd1 = [select id,Name from RecordType where Name ='Manual FPO'];    
ICCWorkbench__c wcb = new ICCWorkbench__c();
wcb.RecordTypeID = RecordTypeIdContact;
wcb.Vendor_Name__c=org.id;
wcb.Name='hchchchchchc2';
wcb.MppsubmittDate__c= Date.newInstance(2014,1,21);
wcb.Organization_ID__c= '82';
wcb.Supplier_Status__c = 'Submitted To Supplier';
insert wcb;
System.debug(wcb);

test.startTest();  

ICC_ReminderEmails b = new ICC_ReminderEmails();
Database.executeBatch(b);

test.stopTest();  


}
}