You need to sign in to do that
Don't have an account?
Rory McDonnell
Trigger on Lead record that updates lead fields and also creates a task
Hi I am learning how to build triggers and am trying to do the following.
When a new lead is created with a specific company name, update two fields on the lead record and also create a new task.
My understanding is that since it is a new record I would need to use an AFTER INSERT trigger as the task will need the newly created lead ID as a reference in the WhatID. But seeing as I also need to update fields on the lead record it doesn't seem that the After Insert will allow that.
Am I stuck with choosing between a Before Insert so I can update my lead record, and an After Insert if I want to create a task.
I'm pretty sure there is a simple solution but still very new to Apex.
When a new lead is created with a specific company name, update two fields on the lead record and also create a new task.
My understanding is that since it is a new record I would need to use an AFTER INSERT trigger as the task will need the newly created lead ID as a reference in the WhatID. But seeing as I also need to update fields on the lead record it doesn't seem that the After Insert will allow that.
Am I stuck with choosing between a Before Insert so I can update my lead record, and an After Insert if I want to create a task.
I'm pretty sure there is a simple solution but still very new to Apex.
Please refer below code:
All Answers
Your understading is absolutely correct, for this you need both Before (to upate Lead fields) and After (to create task) triggers.
Please note that you need to set WhoId when linking task on Lead. :)
Please refer below code: