I'm looking to conditionally update the Opportunity stage when the Task subject (Opportunity & Task belong to same Account) is changed:
Here is the task page, field of concern is Subject:
When the subject is changed to 'Send Email', the Opportunity stage linked to the same account auto updates to 'Qualification', for example. Here, the Opportunity and the Task are for Account ABC:
Now this is what I've done so far; the following is the underdeveloped Apex code to auto update the stage:
public class UpdateOpp {
public static void update_oppstage(task[] tasks) {
for (Task t :tasks) {
if (t.subject == 'Send Letter') {
// update (account) corresponding opportunity stage field to say 'Qualification'
}
}
}
}
And finally, the trigger code:
trigger TaskSubject on Task (after update) {
Task[] tasks = Trigger.new;
UpdateOpp.update_oppstage(tasks);
}
I need help to update the stage to a defined value for the corresponding opportunity. Thanks for your time.
Important Update: My task subject has custom entries in the comboBox – Call EJF, Email Centurion, Follow up Merion, etc. Opportunity names are created meaningfully for an account and the opp name will have one of these (EJF, Centurion, Merion) matching names. So when a task with subject Email EJF is created for account ABC, the opp for the same account ABC with opp name 'EJF' should auto update the opp stage to 'Qualification'.
This is more complex but the basic subject change updates opportunity stage remains unchanged.
Best Answer
Hope the below code helps!
EDIT:
This code is working. I followed the below flow: Create an account(test account) --> Create a task for this account (test account) and make sure the subject is not 'Initiate Merion'.
create an opportunity (make sure the stage is not qualification) for the same account, name it 'test oppty Initiate Merion'.
Now edit the task created, change the subject to 'Initiate Merion'. You can now see that the Opportunity stage has been changed to 'Qualification'.
EDIT 2
Added an after insert event. Also the one more code block to handle the insert and update events seperately
EDIT 3 Task subject need not necessarily be from a set of predefined strings. As long as task subject is part of Opp name, stage must be updated