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
tmbarrytmbarry 

Update Multiple Child records from a Parent Record

I am trying to update a field on a child record - Participation_Level_Tiers__c.Total_Regist__c from a field on the Parent record Account.Total_Registrants__c

 

trigger UpdatePricing on Account (after insert, after update) {
//////////////////////////////////////
// Created 07/18/2013 by Todd Barry
///////////////////////////////////

    for( Account parent: Trigger.new)
    {
    
        List<Participation_Level_Tiers__c> children = [ SELECT Total_Regist__c from 
                                               Participation_Level_Tiers__c where Account__c = :parent.Id];
        
        List<Account> currentacct = [Select Total_Registrants__c from Account 
                                     Where account.id = :parent.Id];
                                               
        List<Participation_Level_Tiers__c> childrenToUpdate = new List<Participation_Level_Tiers__c>();                                               

For (Participation_Level_Tiers__c thischild: children )
    {
    thischild.Total_Regist__c = currentacct[0].Total_Registrants__c;
    childrentoupdate.add(thischild);
    }
IF(childrenToUpdate != null)
{
    update childrentoupdate;
}
}}

 The trigger saves fine, but when I update the Account record nothing happens?  

 

Any thoughts?

 

Also, how would I use a debug statement and where would i put it to see the Account.Total_Registrants__c value being pulled?

 

 

Avidev9Avidev9

Well the code is not optimized, I made few changes, but still the code is not bulk safe but should work

 

trigger UpdatePricing on Account(after insert, after update) {
    //////////////////////////////////////
    // Created 07/18/2013 by Todd Barry
    ///////////////////////////////////
	 List < Participation_Level_Tiers__c > childrenToUpdate = new List < Participation_Level_Tiers__c > ();
    for (Account parent: Trigger.new) {

        List < Participation_Level_Tiers__c > children = [SELECT Id, Total_Regist__c from Participation_Level_Tiers__c where Account__c = : parent.Id ];
        /*
        List < Account > currentacct = [Select Total_Registrants__c from Account
            Where account.id = : parent.Id
        ];
       */
       

        For(Participation_Level_Tiers__c thischild: children) {
            thischild.Total_Regist__c = parent.Total_Registrants__c;
            childrentoupdate.add(thischild);
        }

    }
    IF(!childrenToUpdate.isEmpty()) {
        update childrentoupdate;
    }
}

 

 

 

SaraagSaraag

From the code it looks like you could either use formula field on the child object ?

 

If no, then you can maybe try this: (I did not test this code, but it should work)

trigger UpdatePricing on Account (after insert, after update) {
//////////////////////////////////////
// Created 07/18/2013 by Todd Barry
///////////////////////////////////
Set<Id> accountIds=Set<Id>();
List<Participation_Level_Tiers__c> childrenToUpdate=new List<Participation_Level_Tiers__c>();

for( Account parent: Trigger.new){    
     AccountIds.add(parent.Id);
}

List<Participation_Level_Tiers__c> children = [ SELECT Total_Regist__c,Account__r.Total_Registrants__c from 
                                               Participation_Level_Tiers__c where Account__c IN :accountIds];                                             

For (Participation_Level_Tiers__c thischild: children )
    {
    thischild.Total_Regist__c = thischild.Account__r.Total_Registrants__c;
    childrentoupdate.add(thischild);
    }
IF(childrenToUpdate != null&&childrenToUpdate.size()>0)
{
    update childrentoupdate;
}
}

 and for debug statements, use system.debug(logginglevel.error,'Account TotalReg value:'+parent.Total_Registrants__c); and look for debuglogs under setup