You need to sign in to do that
Don't have an account?
trigger before insert to populate child object field from parent object
Hi I have the trigger below to populate a child field from a parent object field. Upon firing the trigger gives a NULL Pointer error. I have tried severally to for a solution to it but to no avail. Could someone help me with an explanation as to why the error and suggestion to a solution. Thanks.
trigger populateFieldOnCase on Case (before insert){
Set<id>parentIds = new Set<id>();
for(Case acase:trigger.new){
parentIds.add(acase.project__c);
}
Map<id,parent__c> pro = new Map<id,parentt__c>([select id,parentField__c from parent__c where id in :parentIds]);
for(case acase:trigger.new){
acase.childfield__c = pro.get(acase.id).parentField__c;
}
}
Hi,
You have to check condition before you use map.get method.
see below updated trigger.
Apex trigger:
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
All Answers
Hi,
You have to check condition before you use map.get method.
see below updated trigger.
Apex trigger:
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Thanks @Hitesh90 for the response and explanation. It works without errors now.
i have the sam problem, can you help me pleas.:
I have the trigger below to populate a child(Frais_c) field from a parent (Assure_c)object field.
Could someone help me with an explanation as to why the error and suggestion to a solution. Thanks.