You need to sign in to do that
Don't have an account?
cmaine3
Trigger on Task when Complete Field Update
Hi There!
I have a need to update a date field on an opportunity when the task status is changed to completed.Workflow as follows:
Task marked "Completed" > Field "Follow up date" = TODAY() + 5
Pretty simple, but I will be using this trigger as framework for other triggers to get all our reps in line to follow through with a process, so it's pretty important. Thanks anyone who's willing to help.
Try the following code:
trigger updateOpportunityOnTaskComplete on Task (after update) {
public Id oppId;
for(Task t:Trigger.New){
if(t.Status=='Completed'){
oppId = t.WhatId;
}
}
List<Opportunity> opp = [select Id from Opportunity where id = :oppId];
for(Opportunity o : opp){
o.Follow_Up_Date__c = system.today()+5;
update o;
}
}
Let me know if you face any difficulty...................
Hope this helps you.
All Answers
Try the following code:
trigger updateOpportunityOnTaskComplete on Task (after update) {
public Id oppId;
for(Task t:Trigger.New){
if(t.Status=='Completed'){
oppId = t.WhatId;
}
}
List<Opportunity> opp = [select Id from Opportunity where id = :oppId];
for(Opportunity o : opp){
o.Follow_Up_Date__c = system.today()+5;
update o;
}
}
Let me know if you face any difficulty...................
Hope this helps you.
I see a couple problems with your code:
- It will execute regardless of the parent. It's not checking to make sure it's an opportunity task
- If handed a list of tasks, it will only operate on the first one it finds with a "Completed" status, and ignore the remainder
you can check the task before you update the opportunity i.e. whether the trigger's WhatID is opportunity or not.
And moreover it will update if the follow up date on all the opportunity tasks.
You can check : What.Type='Opportunity'
Awesome, works great. I made a few changes. Any comments?