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
pooja chauchanpooja chauchan 

Trigger to update the task status after opportunity

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.
Best Answer chosen by pooja chauchan
Gaurav NirwalGaurav Nirwal

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.

All Answers

@anilbathula@@anilbathula@
Hi pooja chauchan,

try this tigger:-
 
trigger updttask on opportunity(after update){

set<id>ids=new set<id>();
list<task>lsttsk=new list<task>();
 for(opportunity opp:trigger.new){
   if(opp.stagename=='Closed Won'||opp.stagename=='Closed Won'){
     ids.add(opp.id);
   }
}
if(!ids.isempty()){
 list<task>tks=[select id,subject,status from task where whatId IN :ids AND status!='Completed'];
  for(task t:tks){
    t.Status='Completed';
    lsttsk.add(t);
  }
update lsttsk;
}
}


Thanks
Anil.B
Gaurav NirwalGaurav Nirwal

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.
This was selected as the best answer