You need to sign in to do that
Don't have an account?

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);
}
}
}
}
}
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);
}
}
}
}
}
Hi Neeraja,
Please try the following. Also add necessary asserts
Thanks
Vivian
All Answers
Hi Neeraja,
Please try the following. Also add necessary asserts
Thanks
Vivian
Hi Neeraja,
Test classes in Salesforce have a specific template design
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