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
Arek Stegienko DevArek Stegienko Dev 

Trigger help: change parent object field

I'm new to writing Apex code and I need to create a trigger that runs when a task is created and have this trigger update a field in the task's parent object (in this case a lead).  I learn great from examples, so if anyone has a trigger that does exactly this and is willing to post it that would be great.  Or at the very least if you could let me know how I go about getting access to the parent object of the task that would help too.

 

Thanks.

Salesforce WizardSalesforce Wizard

There's some other posts that ask about this.

 

Check out this thread: http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-update-Parent-record-based-on-concatenation-of-child/td-p/268871

 

Basically your steps are to:

 

Loop through all the Child records in trigger.net and build a set of Parent Record IDs

 

Query your Parent Records

 

Loop through parent records and make the change to the field

 

update parent records

 

For your situation Tasks have two "related" fields:

 

WhatID == this is the 'Related To' field

WhoID == this is the Contact/Lead field

 

Normally, The issue is knowing what sObject the WhatID belongs to. Since your situation will always be a lead you need to:

 

1. Loop through Trigger.new (tasks list) and Identify the tasks that are on leads
     one method of doing this is to look at the WhoID of the Task. If the first 3 charactesr of the Id are '00Q' then it's a lead

2. If the WhoID of the task is a lead, Place the WHOId in a Set<Id> "ParentIDSet"

3. Query all Leads where their ID is in ParentIDSet into a list  "LeadList"

4. Loop through LeadList and set the value of the field

5. updateLeadList