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
sw6sw6 

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;

}
}

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

You have to check condition before you use map.get method.

see below updated trigger.

 

Apex trigger:

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,parent__c>([select id,parentField__c from parent__c where id in :parentIds]);
    for(case acase:trigger.new){
        if(pro.get(acase.id) != null){
            acase.childfield__c = pro.get(acase.id).parentField__c;
        }
    }
}

 

 

 

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

hitesh90hitesh90

Hi,

 

You have to check condition before you use map.get method.

see below updated trigger.

 

Apex trigger:

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,parent__c>([select id,parentField__c from parent__c where id in :parentIds]);
    for(case acase:trigger.new){
        if(pro.get(acase.id) != null){
            acase.childfield__c = pro.get(acase.id).parentField__c;
        }
    }
}

 

 

 

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/

This was selected as the best answer
sw6sw6

Thanks @Hitesh90 for the response and explanation. It works without errors now.

shikkishikki
What does <strong> do here?
 
mohand talbimohand talbi
good day;
 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.
trigger insertNameFrais on Frais__c (before insert) {
    
    Set<id>parentIds = new Set<id>();
    
    for(Frais__c f:trigger.new){
        
        parentIds.add(f.ID_Assure__c);// ID_Assure__c master/details in the Frais_c
    }
    
    Map<id, ASSURE__c> pro = new Map<id, ASSURE__c>([select id, Name, ID_Assure__c from ASSURE__c where id in :parentIds]);
    
    for(Frais__c f: Trigger.New){
            
            if(pro.get(f.id) != null){
                
                 String id_ass = pro.get(f.id).ID_Assure__c;
            	 String nom_ass = pro.get(f.id).Name;
            }  
        } 
    }
}