1
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update ) 
{


    List<Task> OpTaskList = New List<Task> () ;


  //  Map<id,Opportunity> OpTaskMap = new Map<id,Opportunity>([Select id, Name, StageName ,
     //   (Select whatid from Tasks where whatid = Null )  
     //  from Opportunity where StageName = 'Closed Won']) ;

    //  System.debug ( 'The Size of the Task Map is ' + ' ' + OpTaskMap.size() );



     For (Opportunity op : Trigger.New)
     {   Task NewTask1 = New Task () ;

      If (Trigger.IsUpdate)
           {   If ( Trigger.oldMap.get(op.Id).StageName != 'Closed Won' 
                 && op.StageName == 'Closed Won')    

                         { 
                            NewTask1.whatid = op.id ;
                            NewTask1.subject = 'Followup Test Task' ;
                            OpTaskList.add(NewTask1) ; }          
             }

            If (Trigger.IsInsert)

             {         If (op.StageName == 'Closed Won')    
                        {   NewTask1.whatid = op.id ;
                            NewTask1.subject = 'Followup Test Task' ;
                           OpTaskList.add(NewTask1) ; }
              }

     }

 If (OpTaskList.size() > 0)
 { Insert OpTaskList ;}   

}
4
  • This works when creating or updating an opportunity from the SF user interface - but still dont know why get this error. – Rajiv V.Subramanian Mar 26 '16 at 11:27
  • 1
    Sorry have solved this myself - the apex challenge needed 'Follow Up Test Task' and the above code had 'Followup Test Task' – Rajiv V.Subramanian Mar 26 '16 at 11:56
  • Apologies for this question :-) – Rajiv V.Subramanian Mar 26 '16 at 11:56
  • Rajiv .... if you solved this question by yourself.. then pls give a answer to your own question and accept that answer.. – Ratan Paul Mar 27 '16 at 6:36

Browse other questions tagged or ask your own question.