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
DScottRDScottR 

Help with a trigger to update field on parent based on value change of field on child (related list)

I have a similar scenario and haved tried adapting the code below to my situtaion but am not familiar enough with triggers to make it work.

 

Here is my situation:

 

Lead = Parent Object

Hot List = Child Object (from a managed install)

 

My main goal is to reference the field "Hot__c", a true/false, from the child object "Hot List" [Hot__List__c.Hot] via an advanced formula.  However this will not work when trying to go down to a child from a parent in the advanced formula builder.

 

So, I figured out I need a trigger to update a custom field "RC_Hot__c" on the parent object "Lead" [Lead.RC_Hot__c].

 

1.  Where should the trigger fire on the Lead or the Child object Hot__List__c?  I want the field Lead.RC_Hot__c to update when the value for  Hot__List__c.Hot__c changes.

 

2.  If the trigger must live on the custom object "Hot__List__c" and that object is from a managed install will this even work?

 

Here is the code that I tried to adapt and it does not work.  try not to laugh I have never done this before ;-)

 

trigger UpdateRCHot on Lead (after insert, after update) {

    // Create a set of Lead IDs to use as a query parameter
    Set<String> LeadIDs = new Set<String>();
    for(Hot__List__c h : Trigger.new) {
        LeadIDs.add(h.Hot_Lead_id__c); // Use whatever your custom look-up field is to Lead from Hot__List__c
    }

    // Create a list of locations to update
    List<Lead> LeadsToUpdate = new List<h.Hot_Lead_id__c>();

    // Select and iterate through any Hot__List__c associated with set of Lead records being inserted or updated
    for(Lead l : [Select Hot_Lead_id__c, Hot__c FROM Hot__List__c WHERE Hot__c = true]) {
    l.RC_Hot__c = false;
    LeadsToUpdate.add(l);
    }

    update LeadsToUpdate;

}