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
Nick MiramsNick Mirams 

Trigger to update the task status after opportunity status is won or lost

Hi,

Can someone help as i am pretty new to all this...

I want to write a trigger on the task which updates the task status to completed after the oipportunity is set to Closed Won or Closed lost.

This is to basically prevent any closed opportunities which still have open tasks.

Its an if statement of some sort but I need to learn how to do all this



Thanks in Advanced.

Nick

Best Answer chosen by Nick Mirams
rohitsfdcrohitsfdc
Hello Nick,
My bad, i re-used the same variable name. Please replace the code with the code below and add the new stagename for opp like you did before.

trigger rohittest on opportunity (after update){

set<id> oppids = new set<id>();
map<id,list<task>> opptotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(opportunity opp : trigger.new){
oppids.add(opp.id);
}

list<task> tasklist = [select whatid,status from task where whatid in : oppids];

for(Task t : tasklist){
if(opptotaskmap.get(t.whatid)!=null){
list<task> temp = opptotaskmap.get(t.whatid);
temp.add(t);
opptotaskmap.put(t.whatid, temp);
}
else
{
opptotaskmap.put(t.whatid, new list<task>{t});
}

}

for(opportunity opp : trigger.new){
if(opp.statename=='Closed Lost' || opp.stagename='Closed Won'){

list<Task> tasklisttemp = opptotaskmap.get(opp.id);

for(task t : tasklisttemp){

	t.status='completed';
taskstoupdate.add(t);
}
	

}

}

if(taskstoupdate.size()>0)
update taskstoupdate;

}


All Answers

rohitsfdcrohitsfdc
Hello Nick,
please try the code below. If that solves your problem, please like this answer and mark this as the best answer.

Thanks,
Rohit

trigger rohittest on opportunity (after update){

set<id> oppids = new set<id>();
map<id,list<task>> opptotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(opportunity opp : trigger.new){
oppids.add(opp.id);
}

list<task> tasklist = [select whatid,status from task where whatid in : oppids];

for(Task t : tasklist){
if(opptotaskmap.get(t.whatid)!=null){
list<task> temp = opptotaskmap.get(t.whatid);
temp.add(t);
opptotaskmap.put(t.whatid, temp);
}
else
{
opptotaskmap.put(t.whatid, new list<task>{t});
}

}

for(opportunity opp : trigger.new){
if(opp.statename=='Closed Lost' || opp.stagename='Closed Won'){

list<Task> tasklist = opptotaskmap.get(opp.id);

for(task t : tasklist){

	t.status='completed';
taskstoupdate.add(t);
}
	

}

}

if(taskstoupdate.size()>0)
update taskstoupdate;

}


Nick MiramsNick Mirams
HI,

Thanks for your help so far....
I got a compile error at line 28 saying duplicate variable?

I am doing this in the test environemnt and also added in another opportunity status of .....|| opp.StageName='Closed No Longer Required'
any ideas?

Thanks for your help

Nick

rohitsfdcrohitsfdc
Hello Nick,
My bad, i re-used the same variable name. Please replace the code with the code below and add the new stagename for opp like you did before.

trigger rohittest on opportunity (after update){

set<id> oppids = new set<id>();
map<id,list<task>> opptotaskmap = new map<id,list<task>>();
list<task> taskstoupdate = new list<task>();
for(opportunity opp : trigger.new){
oppids.add(opp.id);
}

list<task> tasklist = [select whatid,status from task where whatid in : oppids];

for(Task t : tasklist){
if(opptotaskmap.get(t.whatid)!=null){
list<task> temp = opptotaskmap.get(t.whatid);
temp.add(t);
opptotaskmap.put(t.whatid, temp);
}
else
{
opptotaskmap.put(t.whatid, new list<task>{t});
}

}

for(opportunity opp : trigger.new){
if(opp.statename=='Closed Lost' || opp.stagename='Closed Won'){

list<Task> tasklisttemp = opptotaskmap.get(opp.id);

for(task t : tasklisttemp){

	t.status='completed';
taskstoupdate.add(t);
}
	

}

}

if(taskstoupdate.size()>0)
update taskstoupdate;

}


This was selected as the best answer
Nick MiramsNick Mirams
Almost there, just a compile error as its not happy with using || as an OR statement as its not boolean.

Compile Error: OR operator can only be applied to Boolean expressions at line 26 column 36...

I have as the code: -
if(opp.stagename=='Closed Lost' || opp.stagename='Closed Won' || opp.stagename='Closed No Longer Required'){

can you help again you very helpful person?
rohitsfdcrohitsfdc
Nick,
You are using single "=" in comparison, it shoud be double "=" sign. Like below

if(opp.stagename=='Closed Lost' || opp.stagename=='Closed Won' || opp.stagename=='Closed No Longer Required'){

If that solves your query, please like this post and mark this as the best answer.

Thanks
Rohit
Nick MiramsNick Mirams
now im getting a compile error with the name of the trigger

Compile Error: Trigger name exists on different SObject type...

ive changed it a few times and still reveive the same error
may spend some more time learning this stuff.....
rohitsfdcrohitsfdc
try creating a new trigger and post the code there.
Nick MiramsNick Mirams
thanks thats sorted, genius!

think i'm gonna try and learn some more coding, seems fun
rohitsfdcrohitsfdc
Glad i could help. 

Please like the post, and mark it as the best answer if that solves your query.

Thanks,
Rohit
Brian KellerBrian Keller
Hello Rohit,
I found this code very useful and installed it in my sandbox to test.  Worked perfectly in the sandbox but when I tried to deploy to my production org that has the opp pusher intalled, I received the following error:
OppPusherTestsmyOppUnitTestSystem.AssertException: Assertion Failed 
Stack Trace: Class.OppPusherTests.myOppUnitTest: line 37, column 1

Can you offer any help?

Thanks,
​Brian