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
SapanaSapana 

trigger to update field value in parent object with child object's value

Hi,

 

I was writting a trigger to update field value in parent object with child object's value:

Product--- Custom Parent Object

Product Period Data---Custom Child Object

field to update in parent object ---Days_Delinquent__c

 

 

 

trigger UpdateProduct on ENT_NMTC_Product_Period_Data__c (after insert)
{
    
    for(ENT_NMTC_Product_Period_Data__c ppd : System.Trigger.New)
     {           
            ENT_NMTC_Product__c prod = new ENT_NMTC_Product__c();
            
            ppd.NMTC_Product__r.Days_Delinquent__c = ppd.Days_Delinquent__c;
            update(prod); 
            
         
        
     }
   

 

I am not getting any error message in trigger but it does not satisfy the purpose too

Alex.AcostaAlex.Acosta

You'll have to change it to something like such...

 

trigger UpdateProduct on ENT_NMTC_Product_Period_Data__c (after insert){
	
	Set<Id> parentIds = new Set<Id>();
	
	for(ENT_NMTC_Product_Period_Data__c ppd :trigger.new){
		/* 
			This is an example, you'll have to change it to the proper API Field name. 
			I'm guessing this is the name
		*/
		productPeriodDataIds.add(ppd.NMTC_Product__c); 
	}
	
	Map<Id, ENT_NMTC_Product__c> parentMap = new Map<Id, ENT_NMTC_Product__c>([SELECT Id /* add here any other fields you'd like from the parent */ FROM ENT_NMTC_Product__c WHERE Id IN :parentIds]); 
	
    for(ENT_NMTC_Product_Period_Data__c ppd : System.Trigger.New){           
		ENT_NMTC_Product__c prod = parentMap.get(ppd.NMTC_Product__c);
            
		ppd.NMTC_Product__r.Days_Delinquent__c = prod.Days_Delinquent__c;           
    }
	
	if(null != parentMap && parentMap.values().size() > 0){
		update parentMap.values();
	}
}

 

SapanaSapana

Hi ,

 

I have modified the trigger a little but as before it does not update field value of child record into parent record

 

 

rigger UpdateProduct on ENT_NMTC_Product_Period_Data__c (after insert)
{
    
    Set<Id> parentIds = new Set<Id>();
    //Set<Id> productPeriodDataIds = new Set<Id>();
    
    for(ENT_NMTC_Product_Period_Data__c ppd :trigger.new)
    {
        parentIds.add(ppd.NMTC_Product__c); 
    }
    
    Map<Id, ENT_NMTC_Product__c> parentMap = new Map<Id, ENT_NMTC_Product__c>([SELECT Id ,Days_Delinquent__c FROM ENT_NMTC_Product__c WHERE Id IN :parentIds]); 
    
    for(ENT_NMTC_Product_Period_Data__c ppd : System.Trigger.New)
    {           
        ENT_NMTC_Product__c prod = parentMap.get(ppd.NMTC_Product__c);
            
        prod.Days_Delinquent__c = ppd.NMTC_Product__r.Days_Delinquent__c;      
        update prod;
        
     
    }
    if(null != parentMap && parentMap.values().size() > 0)
    {
        update parentMap.values();
        
    }  
    
}

 

 

You can suggest what could be the reason

 

 

Thanks

 

Alex.AcostaAlex.Acosta

This line of code...

prod.Days_Delinquent__c = ppd.NMTC_Product__r.Days_Delinquent__c;      

 Because you are getting information from another Sobject you need to query for it prior... I would do this then

 for(ENT_NMTC_Product_Period_Data__c ppd :[SELECT Id, NMTC_Product__c, NMTC_Product__r.Days_Delinquent__c /* add any other fields you like here */ FROM ENT_NMTC_Product_Period_Data__c WHERE Id IN :trigger.newMap.keySet()])
    {           
        ENT_NMTC_Product__c prod = parentMap.get(ppd.NMTC_Product__c);
            
        prod.Days_Delinquent__c = ppd.NMTC_Product__r.Days_Delinquent__c;      
        update prod;
    }