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
Nagendra Babu 55Nagendra Babu 55 

how to write test class for my class

global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
         return Database.getQueryLocator([SELECT Id, Contact__c, Email__c FROM Issue__c WHERE Contact__c = NULL]);  
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
        // loop through all Issue records and collect email addresses
        // Query Contacts based on Email Addresses
        // Create a Map of Email -- Contact
        
        Set<String> allEmails = new Set<String>();
        for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
            allEmails.add(issue.Email__c);
        }
        
        Map<String, String> contactEmailContactIdMap = new Map<String, String>();
        for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
            contactEmailContactIdMap.put(c.Email, c.Id);
        }
        
        for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
            issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
        }
        Database.update(scope);
    }
    
    global void finish(Database.BatchableContext bc){
        
    }
    

}

 
sowmya thotthadisowmya thotthadi
@isTest
private class classname {
    static testMethod void Method(){
        
        Test.startTest();
         ReportIssueContactAssociationBatch batchobj = new ReportIssueContactAssociationBatch();
          batchobj.execute(null,null);
        Test.stopTest();
    }
}
the basic structure , what i know . can anyone jus help us out
Nagendra Babu 55Nagendra Babu 55
can anyone explain briefly please.,
 
devedeve
Hi Nagendra,

Please try this code

@isTest 
public class AccountUpdateBatchJobTest 
{
    static testMethod void testMethod1() 
    {
        List<Issue__c> lstIssue = new List<Issue__c>();
        for(Integer i=0 ;i <200;i++)
        {
            Issue__c issue = new Issue__c();
            issue.Name ='Name'+i;
            lstIssue.add(issue);
        }
        
        insert lstIssue;
        
        Test.startTest();
            ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
            DataBase.executeBatch(obj);         
        Test.stopTest();
        
        System.AssertNotEquals(null, issue.Contact__c, 'Issue Contact should be filled now');
    }
}
sowmya thotthadisowmya thotthadi
Hi Nagendra ,
I think u are thinking of records which is not there in ur database but tomorrow if u get that record u taught of updating your fields to that record or records on bases of EMAIL ID
 
Nagendra Babu 55Nagendra Babu 55
Yes sowmya , 
     deve can you help me out
 
sowmya thotthadisowmya thotthadi
Error: Variable does not exist: issue
 
@isTest
private class ReportIssueContactAssociationBatchTest  {
   
    static testMethod void testMethod1()
    {
        List<Issue__c> lstIssue = new List<Issue__c>();
        for(Integer i=0 ;i <200;i++)
        {
            Issue__c issue = new Issue__c();
            issue.Name ='Name'+i;
            lstIssue.add(issue);
        }
        
        insert lstIssue;
        
        Test.startTest();
            ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
            DataBase.executeBatch(obj);         
        Test.stopTest();
        
        System.AssertNotEquals(null, issue.Contact__c, 'Issue Contact should be filled now');
}
}
devedeve
Hi Sowmya,

There is some mistake in that. Please check this

 @isTest
private class ReportIssueContactAssociationBatchTest  {
   
    static testMethod void testMethod1()
    {
        List<Issue__c> lstIssue = new List<Issue__c>();
        for(Integer i=0 ;i <200;i++)
        {
            Issue__c issue = new Issue__c();
            issue.Name ='Name'+i;
            lstIssue.add(issue);
        }
        
        insert lstIssue;
        
        Test.startTest();
            ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
            DataBase.executeBatch(obj);         
        Test.stopTest();
        
        System.AssertNotEquals(null, lstIssue.get(0).Contact__c, 'Issue Contact should be filled now');
}
}
sowmya thotthadisowmya thotthadi
Hi deve,
   I think we have reached Nagendra's Criteria
     can u explain him.
 
Nagendra Babu 55Nagendra Babu 55
Thanq all of you for sharing my problem
devedeve
Yes sowmya, You have to create some records of Issue__c object in which contact__c is null and Email__c is filled like this

List lstIssue = new List(); 
Issue__c issue1 = new Issue__c();*
issue1.Name ='test';
issue1.Email__c = test@test.com ;
lstIssue.add(issue1)
Issue__c issue2 = new Issue__c();
issue2.Name ='test1';
issue2.Email__c = test1@test.com ;
lstIssue.add(issue2); i
nsert lstIssue;


Then run the batch using 

ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
DataBase.executeBatch(obj);


Then check in assert that your field Contact__c of Issue__c is updated or not.
Sample Code: 

 @isTest
private class ReportIssueContactAssociationBatchTest  {
   
    static testMethod void testMethod1()
    {
        List lstIssue = new List(); 
        Issue__c issue1 = new Issue__c();*
        issue1.Name ='test';
        issue1.Email__c = test@test.com ;
        lstIssue.add(issue1)
        Issue__c issue2 = new Issue__c();
         issue2.Name ='test1';
        issue2.Email__c = test1@test.com ;
        lstIssue.add(issue2);
         insert lstIssue;
        
        Test.startTest();
            ReportIssueContactAssociationBatch obj = new ReportIssueContactAssociationBatch();
            DataBase.executeBatch(obj);         
        Test.stopTest();
        
        System.AssertNotEquals(null, lstIssue.get(0).Contact__c, 'Issue Contact should be filled now');
}
}
sowmya thotthadisowmya thotthadi
Hi deve ,
  I got one doubt that in Same process if the Contact__c is not filled and Email id not fill where in i want to search by Phone.
   
global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
         return Database.getQueryLocator([SELECT Id, Contact__c, Email__c FROM Issue__c WHERE Contact__c = NULL]);  
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
        // loop through all Issue records and collect email addresses
        // Query Contacts based on Email Addresses
        // Create a Map of Email -- Contact
        
        Set<String> allEmails = new Set<String>();
        Set<String> allPhone = new Set<String>();
        for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
            allEmails.add(issue.Email__c);
        }
        
        Map<String, String> contactEmailContactIdMap = new Map<String, String>();
        Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
        if(Email != Null){
          for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
            contactEmailContactIdMap.put(c.Email, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
          }
            Database.update(scope);
        }else if(Phone !=Null){
         
          for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allPhone]){
            contactEmailContactIdMap.put(c.Phone, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
          }
            Database.update(scope);
     }
  }

    
    global void finish(Database.BatchableContext bc){
        
    }
    

}


 
devedeve
Hi Sowmya,
     You can check this in loop where you are adding values in allEmail list and also you have include this field in query. Please check this and let me know if any doubt


global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
         return Database.getQueryLocator([SELECT Id, Contact__c, Email__c, Phone FROM Issue__c WHERE Contact__c = NULL]);  
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
        // loop through all Issue records and collect email addresses
        // Query Contacts based on Email Addresses
        // Create a Map of Email -- Contact
        
        Set<String> allEmails = new Set<String>();
        Set<String> allPhone = new Set<String>();
        for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
            if(issue.Email__c!=null) {
               allEmails.add(issue.Email__c);
            }
            else {
                allPhone.add(issue.Phone);
            }
        }
        
        Map<String, String> contactEmailContactIdMap = new Map<String, String>();
        Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
        if(allEmails.size()>0){
          for(Contact c : [SELECT id, Email FROM Contact WHERE EMail IN :allEmails]){
            contactEmailContactIdMap.put(c.Email, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
          }
            Database.update(scope);
        }else if(allPhone.size()>0){
         
          for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allPhone]){
            contactEmailContactIdMap.put(c.Phone, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
          }
            Database.update(scope);
     }
  }

    
    global void finish(Database.BatchableContext bc){
        
    }
    

}
Nagendra K 14Nagendra K 14
I wrote a test class for schedule apex can you check for me once ?

//Schedule Apex
global class ReportIssueContactAssociationScheduler implements System.Schedulable {
    global void execute(SchedulableContext sc){
        Database.executeBatch(new ReportIssueContactAssociationBatch());
    }
}


TestClass for above schedule apex


@isTest
private class ReportIssueScheduleTest {
 
    static testmethod void SchedulerTest(){
    
        String xx = ' 0 0 1 16 4 ? *';
        
     // Create your test data
        Contact acc = new Contact();
        acc.FirstName = 'FirstTest';
        insert acc;
         
        Test.startTest();
        
           String jobId = System.schedule('ScheduleApexClassTest',  xx, new ReportIssueContactAssociationScheduler());
       

        // Add assert here to validate result
    }
}
sowmya thotthadisowmya thotthadi
Thanks Deve
Nagendra K 14Nagendra K 14
sowmya or deve or any one help me for my scheduled testing code.
Nagendra K 14Nagendra K 14
hai 
 I got one doubt that in Same process if the Contact__c is not filled where  Last Name and  email is not filled it should search by phone and Last name.

global class ReportIssueContactAssociationBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
         return Database.getQueryLocator([SELECT Id, Contact__c, Email__c, Phone__C,LastName__c FROM Issue__c WHERE Contact__c = NULL]);  
    }
    
    global void execute(Database.BatchableContext bc, List<sObject> scope){
       
        
        Set<String> allEmails = new Set<String>();
        Set<String> allPhone = new Set<String>();
        Set<String> allLastName = new Set<String>();

        for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
            if(issue.Email__c!=null && issue.LastName__c!=null ) {
               allEmails.add(issue.Email__c );
               allLastName .add(issue.LastName__c );
            }
            else if(issue.Phone__c!=null && issue.LastName__c!=null ) {
                 allPhone.add(issue.Phone);
                 allLastName .add(issue.LastName__c ); 
            } 
        }
        
        Map<String, String> contactEmailContactIdMap = new Map<String, String>();
        Map<String, String> contactPhoneContactIdMap = new Map<String, String>();
        Map<String, String> contactLastNameContactIdMap = new Map<String, String>();
        if(allEmails.size()>0 && allLastName.size()>0 ){
          for(Contact c : [SELECT id, Email,LastName__c FROM Contact WHERE EMail IN :allEmails AND LastName IN:allLastName ]){
            contactEmailContactIdMap.put(c.Email, c.Id);
            contactLastNameIdMap.put(c.LastName, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactEmailContactIdMap.get(issue.Email__c);
                issue.LastName__c = contactLastNameContactIdMap.get(issue.LastName__c);
          }
            Database.update(scope);
        }else If (allPhone.size()>0 && allLastName.size()>0){
         
          for(Contact c : [SELECT id, phone FROM Contact WHERE EMail IN :allEmails AND LastName IN:allLastName]){
            contactEmailContactIdMap.put(c.Phone, c.Id);
            contactLastNameIdMap.put(c.LastName, c.Id);
          }
        
          for(sObject so : scope){
            Issue__c issue = (Issue__c)so;
                issue.Contact__c = contactPhoneContactIdMap.get(issue.Phone__c);
                issue.LastName__c = contactLastNameContactIdMap.get(issue.LastName__c);

          }
            Database.update(scope);
     }
  }

    
    global void finish(Database.BatchableContext bc){
        
    }
    

}
devedeve
Hi Nagendra

Then you can use this query
    SELECT id, phone FROM Contact WHERE Phone IN :allPhone AND LastName IN:allLastName
if Email is null

 
alex viksalex viks
I have the same issue, I have read some guide about it on (https://suorinvape.com/products/esco-bars-disposable-2500-puffs)
Makayla ParkerMakayla Parker
I went through the same issue. Read the article below about lost mary (https://www.podkingelfbar.com/products/lost-mary-os5000-elf-bar-disposable-vape-5000-puffs) and follow it step by step.