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
Neeraja SuribhatlaNeeraja Suribhatla 

Test class for before insert before update

Hi,
I need help in writing a test class for the following trigger.  Thank you so much.

trigger childTrigger on child__c (before insert, before update) {
    set<string> OldContactIds = new set<string>();
    map<string,string> mapToParent= new map<string,string>();
    for(child__c childObj : trigger.new){
        if(childObj.CONTACT_OLD_SYSTEM_ID__c !=null){
            OldContactIds.add(childObj.CONTACT_OLD_SYSTEM_ID__c);
            
        }
    }
    if(!OldContactIds.isEmpty()){
        for(Parent__c parentObj :[select id ,OLD_SYSTEM_ID__c from Parent__c where OLD_SYSTEM_ID__c in :OldContactIds]){
            if(parentObj.OLD_SYSTEM_ID__c!=null){
                mapToParent.put(parentObj.OLD_SYSTEM_ID__c,parentObj.id);
                
            }
        }
        for(child__c childObj : trigger.new){
            if(childObj.CONTACT_OLD_SYSTEM_ID__c !=null){
             if(mapToParent.get(childObj.CONTACT_OLD_SYSTEM_ID__c)!=null){
                    childObj.Contact__c = mapToParent.get(childObj.CONTACT_OLD_SYSTEM_ID__c);
                    
                }
            }
        }
    }
    
}
Best Answer chosen by Neeraja Suribhatla
Vivian Charlie 1208Vivian Charlie 1208

Hi Neeraja,

 

Please try the following. Also add necessary asserts

 

@isTest
public class ClassTest{
    public static testmethod void utilitytestACTIVITY() {
		Parent__c objP = new Parent__c();
		objP.OLD_SYSTEM_ID__c = 'ABC';
		objP.Name = 'ABC';
		insert objP;
		
		Parent__c objP1 = new Parent__c();
		objP1.OLD_SYSTEM_ID__c = 'ABC';
		objP1.Name = 'XYZ';
		insert objP1;
		
		child__c objC = new child__c();
		objC.Name = 'Name';
		objC.CONTACT_OLD_SYSTEM_ID__c = 'ABC';
		insert objC;
		
		objC.CONTACT_OLD_SYSTEM_ID__c = 'XYZ';
		update objC;
	}
}


Thanks

Vivian

All Answers

Vivian Charlie 1208Vivian Charlie 1208

Hi Neeraja,

 

Please try the following. Also add necessary asserts

 

@isTest
public class ClassTest{
    public static testmethod void utilitytestACTIVITY() {
		Parent__c objP = new Parent__c();
		objP.OLD_SYSTEM_ID__c = 'ABC';
		objP.Name = 'ABC';
		insert objP;
		
		Parent__c objP1 = new Parent__c();
		objP1.OLD_SYSTEM_ID__c = 'ABC';
		objP1.Name = 'XYZ';
		insert objP1;
		
		child__c objC = new child__c();
		objC.Name = 'Name';
		objC.CONTACT_OLD_SYSTEM_ID__c = 'ABC';
		insert objC;
		
		objC.CONTACT_OLD_SYSTEM_ID__c = 'XYZ';
		update objC;
	}
}


Thanks

Vivian

This was selected as the best answer
Neeraja SuribhatlaNeeraja Suribhatla
Thank you so much Vivian. Can you please explain me why did use utilitytestACTIVITY()? I am new to this. No errors in your test class. 
Vivian Charlie 1208Vivian Charlie 1208

Hi Neeraja,

 

Test classes in Salesforce have a specific template design

@isTest (seAlldata = True/False)
public class testClassName{
public static testmethod void testMethodName() {

}
}

You can have any name for the class or methods. Additionally you create different methods to verify different use cases as and when necessary. For the code snippet shared with you I used utilityTestACTIVITY() as the method name. You could rename it as anything you wish and it should work fine. Hope that helps.

In case your test class works fine please feel free to mark this issue as resolved so that others can also benefit from it.

 

Thanks

Vivian

Neeraja SuribhatlaNeeraja Suribhatla
Thank you very much Vivian. Your code works like a charm.