+ Start a Discussion
niharnihar 

I have created an trigger for 3 tasks when an opportunity record is created but i have to change the due date of the task when ever Probability value changes

Example :

Probability (%) =15%
Then due date should be = due date+15%

My Trigger :
trigger createTask on Opportunity (after insert, after update) {
List<Task> Tasks = new List<Task>();
  if(Trigger.isInsert && Trigger.isAfter){
  for(Opportunity o: Trigger.New){
    Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate-120, WhatId= o.id);
    tasks.add(ts); 
    Task ts1=new Task(subject='stage2',ActivityDate= o.CloseDate-90, WhatId= o.id); 
    tasks.add(ts1); 
    Task ts2=new Task(subject='stage3',ActivityDate= o.CloseDate-80, WhatId= o.id);
    tasks.add(ts2); 
    }
    insert Tasks;
    }
    
if(Trigger.isUpdate && Trigger.isAfter){
system.debug('********');        
for(Opportunity o: Trigger.New){
List<Task> latestRecords = [SELECT Id,Subject,WhatId,ownerId,Status FROM Task WHERE WhatId =: o.Id];
    
system.debug('Tasks:'+latestRecords);
  for(Task t: latestRecords){
  if(o.StageName == '2'){
  if(t.subject == 'stage1'){
  t.Status = 'Completed';
  }
  }
  if(o.StageName == '3'){
  if(t.subject == 'stage2'){
  t.Status = 'Completed';
  }
  }
  if(o.StageName == 'Closed Lost'){
  if(t.subject == 'stage3'){
  t.Status = 'Completed';
  }
  } 
  }
  update latestRecords;
  }
  }
}
Best Answer chosen by nihar
GhanshyamChoudhariGhanshyamChoudhari
for(Opportunity o: Trigger.New){
      if(o.Probability==15 ){
         Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate+15, WhatId= o.id);
    tasks.add(ts);   
      }else{
          Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate-120, WhatId= o.id);
    tasks.add(ts); 
      }
    
    Task ts1=new Task(subject='stage2',ActivityDate= o.CloseDate-90, WhatId= o.id); 
    tasks.add(ts1); 
    Task ts2=new Task(subject='stage3',ActivityDate= o.CloseDate-80, WhatId= o.id);
    tasks.add(ts2); 
    }
    insert Tasks;
    }

 

All Answers

GhanshyamChoudhariGhanshyamChoudhari
for(Opportunity o: Trigger.New){
      if(o.Probability==15 ){
         Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate+15, WhatId= o.id);
    tasks.add(ts);   
      }else{
          Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate-120, WhatId= o.id);
    tasks.add(ts); 
      }
    
    Task ts1=new Task(subject='stage2',ActivityDate= o.CloseDate-90, WhatId= o.id); 
    tasks.add(ts1); 
    Task ts2=new Task(subject='stage3',ActivityDate= o.CloseDate-80, WhatId= o.id);
    tasks.add(ts2); 
    }
    insert Tasks;
    }

 
This was selected as the best answer
niharnihar
hi Ghanshyam,
Thank you for your code but probability value will not remain 15 it can be changed to any value and due date must be updated...................
GhanshyamChoudhariGhanshyamChoudhari
Task ts=new Task(subject='stage1',ActivityDate= o.CloseDate+o.Probability, WhatId= o.id);
you can try this
niharnihar
Hi Ghanshyam ,
The code you send is showing errors (Date expressions must use Integer or Long)
arpit vijayvergiyaarpit vijayvergiya
Try this 
Date activityDate = o.CloseDate.addDays(o.Probability*100);
Task ts=new Task(subject='stage1',ActivityDate= activityDate, WhatId= o.id);
niharnihar
HI arpit,
by using your code i am getting error (Method does not exist or incorrect signature: void addDays(Decimal) from the type Date)