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
Lakshmi SLakshmi S 

Update Parent Object status based Task ?

Hi Team,
Requirement : Update Parent Object status based Task ?
Parent Object - A
Child Object - Task
Suppose one parent record has 2 tasks, if the 2 tasks status is completed then update status on parent A to completed other wise update to pending..
How can we do this using trigger or some other way.

Please let me know.

Thanks,
Lakshmi
 
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hi Lakshmi S,
trigger childTrigger on Contact(after update) {

 Set <ID> parentSet = new Set <ID>();
 for(Contact c : Trigger.new)
 {
  parentSet.add(c.AccountId);
 }

 List <Account> accList = [SELECT Id, Parent_Status__c, (SELECT Id, Child_Status__c FROM Contacts) FROM Account WHERE Id IN : parentSet];

 for(Account a : accList)
  {
   integer count=0;
   for(Contact c : a.Contacts)
   {
     if(c.Child_Status__c != 'Completed')
     {
        a.Parent_Status__c = 'Pending';
        break;
     }
     else
      {
            count++;
      }
    }
     if(a.contacts.size == count)
     {
          a.Parent_Status__c = 'completed';
     }
    }
update accList;
}
you can go through the above code,in which contact is child object and account is master object ,you can change the code with your parent and child object.
hope this will help you,
if it solve your problem please mark it as a best answer.
thanks,
 
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hi Lakshmi S,
A small correction in the above code which is 
If(a.contacts.size() == count)
contacts is relationship field.so you can use use your relationship field.
thanks,