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

apex trigger

I have written a trigger which works fine if , i have task created after an contact is created . But I have a situation where a task is created at the same time when an contact record is created .So, it doesn't allow me to update the contact record.

I have created a custom field in contact for counting the total open activities.

i would really appreciate any help in solving this...thank you


trigger CountContact on Task (after insert,after update  )
        if(Trigger.IsUpdate || Trigger.IsInsert)
       String ContactId = Trigger.New[0].whoId;  
       if(ContactId.substring(0,3) =='003')
            List<Task> Tasks = [Select Id from Task where Status =: 'Not Started' and whoId =: ContactId ];
            Contact C = [Select id,Total_open_Activity__c from Contact where Id =:Trigger.New[0].WhoId]  ;
            C.Total_open_Activity__c = Tasks.size();
         //   update C;     




You can collect the Contact records Ids in a list and  pass it to the @future method and update the contact records in the @future methods.


Even then, if the triggers are recursively called,  check for some condition before updating the contact record.


Hope this helps. If so, Please mark the solution as solved.