How to stop the creating a task when opportunity stage = closed won? If we trying to create a task for closed opportunities we should fire a Error message like "You can not add a task on opportunity if the status is Completed".
we have written a trigger but not working properly.Please find the below trigger and let us where we made the mistakes.
trigger CreateTask on Opportunity(after insert,after update,before insert){
List<Opportunity> listOpp = Trigger.new;
List<Task> listTask = new List<Task>();
if(Trigger.isInsert){
for(Opportunity opp:listOpp){
if(opp.Need__c != null && opp.StageName=='AC5-Closed Won'){
Task t = new Task();
t.OwnerId = opp.OwnerId;
t.WhatId = opp.Id;
t.Subject ='Help' + ': ' +opp.Need__c + ': ' + opp.Name;
//t.ActivityDate = opp.Need__c;
t.ActivityDate = System.Today()+7;
t.Description = opp.Description__c;
t.Status = 'In Progress';
t.Priority = 'Normal';
listTask.add(t);
}
else{
t.addError('You can not add a task on opportinty if the status is Completed or Closed Lost or Abandon');
}
}
}
}