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
salesforce_hoonigansalesforce_hoonigan 

TRIGGER: update child records when parent record is updated

Hi All,

Please bear with me. I am an APEX newbie and needed some assistance please.

Goal: I wanted to update the checkbox (SentForApproval__c) in Child Object (Rate_sheet__c) to TRUE if the Parent Record (Lead) Status (Lead_Status_NEW__c) was set to "CA Completed". This is a lookup relationship.

Parent Object: Lead
Child Object: Rate_sheet__c

Here's what I have so far and I am unsure how to complete the code or even correct this:
 
trigger CAforApproval on Lead (after update){

	set<Id> leadIds = new set<Id>();
    	map<Id, Lead> mapLead = new map<Id, Lead>();
    	list<Rate_sheet__c> listRateSheet = new list<Rate_sheet__c>();
    
    	for(Lead lead : trigger.new) {
        	leadIds.add(lead.Id);
        	mapLead.put(lead.Id, lead);
        }
    
   	 listRateSheet = [SELECT SentForApproval__c,  Lead__r.Id  FROM Rate_sheet__c WHERE Lead__r.Id  IN : leadIds];
    
    if(listRateSheet.size() > 0) { 
        for(Rate_sheet__c rs : listRateSheet) {

Any help is greatly appreciated. Thank you
Best Answer chosen by salesforce_hoonigan
pconpcon
Whoops, my fault.  This should do what you want.
 
trigger CAforApproval on Lead (after update){
    Set<Id> leadIds = new Set<Id>();
    List<Rate_Sheet__c> rateSheetsToUpdate = new List<Rate_Sheet__c>();

    for (Lead lead: Trigger.new) {
        Lead oldLead = Trigger.oldMap.get(lead.Id);

        if (
            lead.Lead_Status_NEW__c != oldLead.Lead_Status__c &&
            lead.Lead_Status_NEW__c == 'CA Completed'
        ) { 
            leadIds.add(lead.Id);
        }
    }

    for (Rate_sheet__c rateSheet: [
        select SentForApproval__c,
            Lead__c
        from Rate_sheet__c
        where Lead__c in :leadIds
    ]) {
        if (rateSheet.SentForApproval__c != true) {
            rateSheet.SentForApproval__c = true;
            rateSheetsToUpdate.add(rateSheet);
        }
    }

    if (!rateSheetsToUpdate.isEmpty()) {
        update rateSheetsToUpdate;
    }
}

 

All Answers

pconpcon
This is a pretty simple trigger.  You can do the following:
 
trigger CAforApproval on Lead (after update){
    List<Rate_Sheet__c> rateSheetsToUpdate = new List<Rate_Sheet__c>();

    for (Rate_sheet__c rateSheet: [
        select SentForApproval__c,
            Lead__c
        from Rate_sheet__c
        where Lead__c in :trigger.newMap.keySet()
    ]) {
        if (rateSheet.SentForApproval__c != true) {
            rateSheet.SentForApproval__c = true;
            rateSheetsToUpdate.add(rateSheet);
        }
    }

    if (!rateSheetsToUpdate.isEmpty()) {
        update rateSheetsToUpdate;
    }
}
salesforce_hoonigansalesforce_hoonigan
Hi Pcon,

Thanks for the help. However, it triggers regardless of what Lead Status I set. It should only trigger once I set the Lead_Status_NEW__c to "CA Completed".

Thanks in advance.
pconpcon
Whoops, my fault.  This should do what you want.
 
trigger CAforApproval on Lead (after update){
    Set<Id> leadIds = new Set<Id>();
    List<Rate_Sheet__c> rateSheetsToUpdate = new List<Rate_Sheet__c>();

    for (Lead lead: Trigger.new) {
        Lead oldLead = Trigger.oldMap.get(lead.Id);

        if (
            lead.Lead_Status_NEW__c != oldLead.Lead_Status__c &&
            lead.Lead_Status_NEW__c == 'CA Completed'
        ) { 
            leadIds.add(lead.Id);
        }
    }

    for (Rate_sheet__c rateSheet: [
        select SentForApproval__c,
            Lead__c
        from Rate_sheet__c
        where Lead__c in :leadIds
    ]) {
        if (rateSheet.SentForApproval__c != true) {
            rateSheet.SentForApproval__c = true;
            rateSheetsToUpdate.add(rateSheet);
        }
    }

    if (!rateSheetsToUpdate.isEmpty()) {
        update rateSheetsToUpdate;
    }
}

 
This was selected as the best answer
salesforce_hoonigansalesforce_hoonigan
Thanks Pcon! it Works great! I appreciate the help.
ALISHA nagarkar 10ALISHA nagarkar 10
Hi, 

Please help me I have one custom object project and standard object opportunity,
I am creating the project fields in Opportunity while creating new opportunity.
Now, i want to update the project fields in opportunity when we make changes in project fields 
tania chattopadhyaytania chattopadhyay

Hi pcon,

The code is absolutely helpful but would like to know if I want to update only the new version of child records when the parent is updated. How would I compare the new version of child records and update only those.?

I have a scenario, So when ever an Account is updated, a new Contact is getting created under that Account. And I want the new Contacts to be updated not the contactswhich are already been craeted earlier under the same Account. I want new version of contacts to be updated.

Can you plz help me with this? Thanks in advance!

 

pconpcon
Not sure what exactly you mean by "new contacts to be updated" if you are creating these contacts in a trigger then you would just set the data you need before creating them.  If you are instead trying to find the most recent contact created under an account and update that one, then it's a bit more complicated.