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
sales4cesales4ce 

Update Contact based on Tasks

Hi ,

 

I have a custom field on my Contact, which tracks of any open tasks.The field is called " Tasks_Pending__c". Its a Picklist with two values "yes" or "No".

Assume that i have  2 tasks associated a contact.

 

Task 1 has a status as "Completed" and Task 2 has a Status as "Not started".

 

then Tasks_Pending__c should have a Value "Yes", if both task status is equal to "completed", it should be "No".

 

I have written a trigger, but could not accomplish as i got stuch.Can any one help me with this.

 

trigger Check_Tasks on Contact (after Insert, after Update) 
{
    List<Id> Contact_Ids= new List<Id>();
    
    for(Contact c: Trigger.New)
    {
        Contact_Ids.add(c.Id);
    }
    
    List<Task> getcontact_Tasks=[Select Id,Status,WhoId from Task where WhoId IN :Contact_Ids and Status='Not Started'];
  
      List<Contact> ct=new List<contact>();  
    if(getcontact_Tasks.Size()>0)
    {
    
    }

 

Thanks,

Sales4ce

Happy2HelpHappy2Help

Hi

 

I want to tell you some things...

 

First of all after seeing your senario your apporach is wrong.

 

If you want to update 'Tasks_Pending__c' Field value in Contact, You should write a Trigger on  "Task" not on Contact. Suppose 2 new Tasks were created for a Contact and you will see the status of 'Tasks_Pending__c' is "No" (if it was No previously), and this will only update when you will update this contact.

 

In any Case you want to write Trigger on Contact in this senario You have to  update yr records under @future method.

 

Any ways i have write a code for you Hope this will fullfill your requirment.

trigger Contact_Update on Task (after insert, after update)
{
  // Set Don't allow Duplicate Value.
  Set<Id> setContact = new Set<Id>();
  for(Task ts: Trigger.new)
   {
     //All the Contacts That have any of the task that is not completed yet.
     if(ts.Status !='completed') 
       setContact.add(ts.WhoId);
   }
   
   List<Contact> lstContact = [select id,Tasks_Pending__c from contact where Id in :setContact Limit 1000];
   
   //Updating All the Contacts with status 'Yes'
   for(Contact con: lstContact)
    {
     con.Tasks_Pending__c = 'Yes';
    }
    update lstContact;
}

 

Thanks

 

JAW99JAW99

Hello am trying to do something similar, have a completed task by certain profiles update a custom last action date on the account., Can you help? thanks