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
Mike DeMilleMike DeMille 

Apex Trigger Help

I need help with the general steps in creating an apex trigger.  I'm a beginner to apex coding, and have only done very basic field updates and record creations.  I need to build an apex trigger that updates a lead's status (a picklist value) to 'contacted' when the lead rating (also a picklist field) has either "D1", "D2", "D3", or "D4" selected AND the lead has been contacted via email (noted through activity with subject containing "Message Sent:")

So basically here is the criteria:  

IF a lead has status = "OPEN" AND lead rating= D1orD2orD3orD4 AND there is an activity associated with the lead with subject containing "Message Sent"

THEN update lead status to "Contacted"

I know this is a fairly basic trigger, but I could use some help with the architecture of what needs to be included. I would really appreciate any help in getting this going.  

Matthew CokeMatthew Coke

First thing you'll want is the structure of the trigger. I'd recommend using one of these sources as a guide.

https://developer.salesforce.com/forums/?id=906F000000093cKIAQ
http://www.sfdc99.com/2015/01/19/the-one-trigger-per-object-design-pattern/

After that is done, then the logic should be fairly simple to plug in.

Try building a collection (a list for example) and then adding items to that collection based on your required logic (if status == open && rating == ...etc)