1

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');
            }

        }
    }
}
6
  • 1
    Firstly, you cannot add error on Task when your trigger is written on Opportunity.Secondly, please brief out the requirement properly.Your code allows to create task when Opp is closed won and questions's language states the opposite
    – Abhijeet
    Aug 9, 2016 at 15:55
  • @Abhijeet, My requirement is whenever opportunity fields are updating create a new brand task with opportunity name.For that above code is working fine but we want to stop the task creation when Opportunity stage name is closed. how to achieve this one...Thanks
    – Chinna
    Aug 9, 2016 at 16:00
  • You can't insert Tasks during before insert trigger on Opportunity because there is no Opportunity.ID value to make as parent of Task. I also agree with Abhijeet - the code only creates tasks when Opportunity is closed won
    – cropredy
    Aug 9, 2016 at 16:04
  • @cropredy: Can you please update me where i need to change the logic on my existing trigger.. Thanks
    – Chinna
    Aug 9, 2016 at 16:10
  • Chinna - we're confused. What is the requirement -- when an oppo is created/updated - you want to automatically create a Task (unless Oppo is closed won)? OR are you trying to prevent a Task from being created by the user when clicking New Task if Oppo is closed won?
    – cropredy
    Aug 9, 2016 at 16:24

1 Answer 1

0

From the description it appears you may have two requirements

1 - Automatically create Tasks on Open Opportunities (but not closed)

trigger CreateTask on Opportunity(after insert,after update){
 // Adds Tasks to each open Oppo on insert or relevant update
 List<Task> tasksToUpdate = new List<Task>();

 for(Opportunity opp: Trigger.new){
   if ( opp.StageName != 'AC5-Closed Won' &&  // consider only open Oppos
        (Trigger.isInsert ||                  // newly inserted or
        (Trigger.isUpdate &&                  // on update, some relevant fld has changed
           opp.Foo__c != Trigger.oldMap.get(opp.id).Foo__c ||
           opp.Bar__c != Trigger.oldMap.get(opp.id).Bar__c
           ..)  // wasn't clear what role Need__c played but could be added to this if stmt
        ) 
      )               
        tasksToUpdate.add(new Task(
            OwnerId = opp.OwnerId,
            WhatId = opp.Id,
            Subject ='QMIHelp' + ': ' +opp.Need__c + ': '  + opp.Name, 
            ActivityDate = System.Today().addDays(7),  
            Description = opp.Description__c,
            Status = 'In Progress',
            Priority = 'Normal');
 }
 insert tasksToUpdate;  // no need to check for empty list       
}

2 - Prevent Tasks from being added to Closed-won Opportunities

Exercise left for the reader - but basically you will need a before insert trigger on Task. There's a code sample here on Developer Forum that is helpful

2
  • Thanks for your help. Now i have understood. Once again thanks
    – Chinna
    Aug 10, 2016 at 15:23
  • Glad I could help, once you confirm the solution, as a courtesy to the Community mark the answer as solved
    – cropredy
    Aug 10, 2016 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .