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
Anonymous DeveloperAnonymous Developer 

Need Help Test Class

Here's my code
global class AS_UpdateContactFromStagingObject implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT AS_Contact_Id__c, AS_Pronouns__c, AS_Aboriginal_or_Torres_Strait_Islander__c, AS_Date_of_Birth__c, AS_CALD__c, AS_Vaccination_status__c, AS_Require_an_interpreter__c, AS_TIS_Interpreter_Language_Required__c, AS_Safe_to_use_this_phone_number__c, AS_Is_it_safe_to_use_this_email_address__c, AS_Can_a_voicemail_be_left__c, AS_Street__c, AS_City__c, AS_State_Province__c, AS_Zip_Postal_Code__c, AS_Country__c, AS_Why_is_this_their_current_location__c, AS_Agency_Referral__c, AS_Contact_Updated__c FROM Contact_Staging__c WHERE AS_Agency_Referral__c = True AND AS_Contact_Updated__c = False AND AS_Contact_Id__c != null';
        return Database.getQueryLocator(query);

    }
    global void execute(Database.BatchableContext BC, List<Contact_Staging__c> batch) {

        System.debug('batch=='+batch);
        
        
        List<Id> conList = new List<Id>();
        
        for(Contact_Staging__c a : batch){
            conList.add(a.AS_Contact_Id__c);
        }
        
        Map<Id, Contact> mapcontact = new Map<Id, Contact>();
        List<Contact> contacList = new List<Contact>();
        
        for( Contact mapcon: [SELECT Id,caseman__Pronouns__c,AS_Aboriginal_or_Torres_Strait_Islander__c,Birthdate,AS_CALD__c,AS_Vaccination_status__c,
                              AS_TIS_Interpreter_Language_Required__c,AS_Safe_to_use_this_phone_number__c,AS_Is_it_safe_to_use_this_email_address__c,
                              AS_Can_a_voicemail_be_left__c,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,AS_Why_is_this_their_current_location__c
                              FROM Contact WHERE Id IN :conList]){
                                  System.debug('mapcon==' + mapcon);
                                  mapcontact.put(mapcon.Id, mapcon);
                              }
		System.debug('mapcontact==' + mapcontact);
        
        for(Contact_Staging__c constage : batch){
            Contact conTemp = mapcontact.get(constage.AS_Contact_Id__c);
            
            if(constage.AS_Pronouns__c != null){
                conTemp.caseman__Pronouns__c = constage.AS_Pronouns__c;
            }
            if(constage.AS_Aboriginal_or_Torres_Strait_Islander__c != null){
                conTemp.AS_Aboriginal_or_Torres_Strait_Islander__c = constage.AS_Aboriginal_or_Torres_Strait_Islander__c;
            }
            if(constage.AS_Date_of_Birth__c != null){
                conTemp.Birthdate = constage.AS_Date_of_Birth__c;
            }
            if(constage.AS_CALD__c != null){
                conTemp.AS_CALD__c = constage.AS_CALD__c;
            }
            if(constage.AS_Vaccination_status__c != null){
                conTemp.AS_Vaccination_status__c = constage.AS_Vaccination_status__c;
            }
            if(constage.AS_TIS_Interpreter_Language_Required__c != null){
                conTemp.AS_TIS_Interpreter_Language_Required__c = constage.AS_TIS_Interpreter_Language_Required__c;
            }
            if(constage.AS_Safe_to_use_this_phone_number__c != null){
                conTemp.AS_Safe_to_use_this_phone_number__c = constage.AS_Safe_to_use_this_phone_number__c;
            }
            if(constage.AS_Is_it_safe_to_use_this_email_address__c != null){
                conTemp.AS_Is_it_safe_to_use_this_email_address__c = constage.AS_Is_it_safe_to_use_this_email_address__c;
            }
            if(constage.AS_Can_a_voicemail_be_left__c != null){
                conTemp.AS_Can_a_voicemail_be_left__c = constage.AS_Can_a_voicemail_be_left__c;
            }
            if(constage.AS_Street__c != null){
                conTemp.MailingStreet = constage.AS_Street__c;
            }
            if(constage.AS_City__c != null){
                conTemp.MailingCity = constage.AS_City__c;
            }
            if(constage.AS_State_Province__c != null){
                conTemp.MailingState = constage.AS_State_Province__c;
            }
            if(constage.AS_Zip_Postal_Code__c != null){
                conTemp.MailingPostalCode = constage.AS_Zip_Postal_Code__c;
            }
            if(constage.AS_Country__c != null){
                conTemp.MailingCountry = constage.AS_Country__c;
            }
            if(constage.AS_Why_is_this_their_current_location__c != null){
                conTemp.AS_Why_is_this_their_current_location__c = constage.AS_Why_is_this_their_current_location__c;
            }
            contacList.add(conTemp);
        }
        
        update contacList;
    }

    global void finish(Database.BatchableContext BC){

    }
}

Thanks.
Best Answer chosen by Anonymous Developer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class which gives you 100% coverage. make sure you add all the required fields and other fields in Contact_Staging__c object while creating the record.
 
@istest
public class AS_UpdateContactFromStagingObjectTest {
static testMethod void myTest() {
    Contact con= new Contact();
    con.lastname='sample contact';
    insert con;
    Contact_Staging__c csstag= new Contact_Staging__c();
    csstag.AS_Contact_Id__c=con.id;
    csstag.AS_Date_of_Birth__c= system.today();
    csstag.AS_Agency_Referral__c=True;
    csstag.AS_Contact_Updated__c=false;
    insert csstag;
    
    AS_UpdateContactFromStagingObject bs= new AS_UpdateContactFromStagingObject();
    database.executeBatch(bs);
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class which gives you 100% coverage. make sure you add all the required fields and other fields in Contact_Staging__c object while creating the record.
 
@istest
public class AS_UpdateContactFromStagingObjectTest {
static testMethod void myTest() {
    Contact con= new Contact();
    con.lastname='sample contact';
    insert con;
    Contact_Staging__c csstag= new Contact_Staging__c();
    csstag.AS_Contact_Id__c=con.id;
    csstag.AS_Date_of_Birth__c= system.today();
    csstag.AS_Agency_Referral__c=True;
    csstag.AS_Contact_Updated__c=false;
    insert csstag;
    
    AS_UpdateContactFromStagingObject bs= new AS_UpdateContactFromStagingObject();
    database.executeBatch(bs);
}
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Anonymous DeveloperAnonymous Developer
Thanks Sai