Ask Search:
Ron JenkinsRon Jenkins 

I need a trigger to update all child records of an Opportunity. Any help?

I have a custom object called Related_Deals__c with a lookup into Opportunity records. When an Opportunity (parent) Stage is changed, I need to field update all the Related_Deals__c (child) records with the Stage value. Anybody have a code example for this?
Best Answer chosen by Moderator (salesforce.com) 
Joey - CloudJediJoey - CloudJedi
trigger updateRelatedDealRenewalStage on Opportunity (after update, after insert){
     
    Set<ID> oppids = new Set<ID>();
    RecordType debtRT = [Select Id From RecordType Where Name = 'Debt - Renewal'];
    for(Opportunity opp : Trigger.new){
        // Check for Renewal Deal Record Type
        if(opp.RecordTypeId == debtRT.Id){
            oppids.add(opp.Id);
        }
    }
    List<Opportunity> updatedOpps = [SELECT Id, StageName, (Select Id, Renewal_Deal__c, Renewal_Stage__c from Related_Deals__r)  FROM Opportunity WHERE Id in :oppids];
 
    List<Related_Deals__c> relatedDealsToUpdate = new List<Related_Deals__c>();
    
    for (Opportunity opp : updatedOpps){
        // Loop through each Related Deal record
        for(Related_Deals__c rd : opp.Related_Deals__r){
            rd.Renewal_Stage__c = opp.StageName;
            relatedDealsToUpdate.add(rd);
        }
    }
    update relatedDealsToUpdate;
 }

All Answers

Subramanyam MeareSubramanyam Meare
Which Edition of Salesforce are you using?If you are using higher than Professional Edition you could achieve it with Wokflow rule.
Jayant JoshiJayant Joshi
If you are on EE, UE or DE, You can do this using a trigger. let me know if you need more help.
Jayant JoshiJayant Joshi
For a sample code similar to your requirement, you may refer to this:

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-update-child-records-on-Opportunity-Compile-Error/td-p/366243


This is a simple code which update child field based on a parent's field value.
Sanjay mankaniSanjay mankani
First in a "Set", get all IDS of parent Object. Then using that "Set", get all child object records. Now create a map "Map<ParentId,List<Child Records>>" using fetched child records. Now start a loop on your parent object and from there get a ID of parent record and get child records of that parent by using created "Map" and then update those child record values.
Joey - CloudJediJoey - CloudJedi
+1 to Jayant's answer.

Could you check if you're on master-detail relationship? You might be able to use a workflow rule for this.
Ron JenkinsRon Jenkins
We are on Enterprise Edition. I have tried a couple different trigger code examples but cannot sem to get it to work. I am new to Triggers and would approciate any help. The requirements are simple enough, it seems someone would have done this before. .... hopefully :-)

The child object actually has 2 lookup relationships with the master (Opportunity). Opportunities represent loans. There is an original loan and often a renewal of the loan. I need to link the renewal to the original loan or loans. Because there may be several origianl loans bundled to a renewal, I need a custom related object to tie them together. Therefore, I have a lookup to the renewal and master-detail to the original loans. IF I can trigger an update to the child recrods, then I can field update the priginal Opportunity records as "Renewed."
Joey - CloudJediJoey - CloudJedi
Hi Ron,

Could you post your current trigger code? Alternatively feel free to contact me via my profile if you need assistance.

--
Ron JenkinsRon Jenkins
THANK YOU FOR YOUR HELP!

My code so far...
//Opportunity is the parent object and Related_Deals__c is the child object
// They are linked by a lookup field Renewal_Deal__c
// For Record Type ID '012U00000009QIv' I need to copy the StageName from Opportunity to a text field Renewal_Stage__c.

trigger updateRelatedDealRenewalStage on Opportunity (after update) {
     
     Set<ID> ids = new Set<ID>();
     list<Opportunity> updOpps = 
     [SELECT Id, 
                 (Select Id, Renewal_Deal__c, Renewal_Stage__c 
                  from Related_Deals__r ) FROM Opportunity 
                  WHERE Id in :ids];         
                 
                 List<Related_Deals__c> RDToUpdate = new List<Related_Deals__c>();
 
         for (Opportunity Opp : updOpps) 
            { 
// Check for Renewal Deal Record Type
             if(opp.RecordTypeId == '012U00000009QIv')
             
               for(Related_Deals__c rd : Opp.Related_Deals__r) 
               { 
       rd.Renewal_Stage__c = opp.StageName;
       RDToUpdate.add(rd);
       update RDToUpdate;
                    
                }
            }
 
}
Joey - CloudJediJoey - CloudJedi
Hi Ron,

Could you contact me via my profile to see if we can debug your current code?

One thing that I noticed is that the "update RDToUpdate;" shouldn't be in the loop. It should be outside the "for (Opportunity Opp : updOpps)" loop.

What problems are you having with it?

Thanks!

--
Joey - CloudJediJoey - CloudJedi
trigger updateRelatedDealRenewalStage on Opportunity (after update, after insert){
     
    Set<ID> oppids = new Set<ID>();
    RecordType debtRT = [Select Id From RecordType Where Name = 'Debt - Renewal'];
    for(Opportunity opp : Trigger.new){
        // Check for Renewal Deal Record Type
        if(opp.RecordTypeId == debtRT.Id){
            oppids.add(opp.Id);
        }
    }
    List<Opportunity> updatedOpps = [SELECT Id, StageName, (Select Id, Renewal_Deal__c, Renewal_Stage__c from Related_Deals__r)  FROM Opportunity WHERE Id in :oppids];
 
    List<Related_Deals__c> relatedDealsToUpdate = new List<Related_Deals__c>();
    
    for (Opportunity opp : updatedOpps){
        // Loop through each Related Deal record
        for(Related_Deals__c rd : opp.Related_Deals__r){
            rd.Renewal_Stage__c = opp.StageName;
            relatedDealsToUpdate.add(rd);
        }
    }
    update relatedDealsToUpdate;
 }
This was selected as the best answer
Joey - CloudJediJoey - CloudJedi
Glad its working now Ron! :)

--
Christine PechterChristine Pechter
Hello @Joey,
 
I saw you posted the accepted answer to this question...
 
I am basically trying to accomplish the exact same thing that Ron was looking to accomplish (update a child record when a field on the parent is filled in), however when I replace your code with my objects, there is an error.  I was wondering if you had thoughts about this?  I have listed the error below, along with my code, and also flagged where the error is occurring with a comment // **.  Thank you!
 
***"Compile Error: Didn't understand relationship 'Named_Party__r' in FROM part of query call."
 
//Contract is the parent object and Named_Party__c is the child object
//They are linked by a lookup field Related_Contract__c
//For Record Type ID 'PSA', I need to copy the Date_Trx_Sheet_Generated__c from the Contract to a date field Date_Transferred_to_Implementation__c on the Named_Party.
 
trigger UpdateNamedPartyTransferredDate on Contract (after update, after insert){
     
    Set<ID> cids = new Set<ID>();
    RecordType cRT = [Select Id From RecordType Where Name = 'PSA'];
    for(Contract c : Trigger.new){
        // Check for PSA Record Type
        if(c.RecordTypeId == cRT.Id){
            cids.add(c.Id);
        }
    }
 
// **** THE ERROR IS OCCURING IN THE NEXT PART OF THE CODE
 
    List<Contract> updatedContracts = [SELECT Id, Date_Trx_Sheet_Generated__c, (Select Id, Related_Contract__c, Date_Transferred_to_Implementation__c FROM Named_Party__r)  FROM Contract WHERE Id in :cids];
 
    List<Named_Party__c> relatedNamedPartiestoupdate = new List<Named_Party__c>();
    
    for (Contract c : updatedContracts){
        // Loop through each Named Party record
        for(Named_Party__c dt : c.Named_Party__r){
            dt.Date_Transferred_to_Implementation__c = c.Date_Trx_Sheet_Generated__c;
            relatedNamedPartiestoupdate.add(dt);
        }
    }
    update relatedNamedPartiestoupdate;
 }
Joey - CloudJediJoey - CloudJedi
Hi Christine, would you be able to contact me via my profile to see how we
can help? Thanks!
DrupalDrupal
Hi Joey,

Would you please provide the unit test code for this?

Thanks a lot.