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
sfdc coolcrmsfdc coolcrm 

Triggers to update parent record status to completed when all its child is status is completed.

Hi,
I am new to apex trigger and got stuck while doing a requirement to wite trigger to update parent record status to completed when all its child is status is completed. Paretnt is a custom object and child (task) is sobject. The issue is folloiwng trigger always update the parent status to 'Completed' whenever a child status is set to 'Completed' eventhough there are child records which are not in completed status. Expected here is since there are not 'Completed' child records exist, parent status should not have been updated to completed. Any help is greately appriciated.  

trigger UpdateMilestoneStatus on Task (after insert, after update) {
    Set<String> whatId = new Set<String>();
    Integer counter = 0;
    for (Task t : Trigger.new) {
        whatId.add(t.WhatId);
    }
    List<Milestone__c> milestn = [SELECT Id, Status__c FROM Milestone__c WHERE Id =: whatId];
  
    for (Task t : Trigger.new){
                   
              
                if(t.Status != 'Completed')
                {
                  counter++;
                }
            
        }
         
    for (Milestone__c c: milestn) {
        
        
            c.Status__c = 'Completed';
           
   
    }
    
    if (counter == 0){
    update milestn;}

}
Best Answer chosen by sfdc coolcrm
Art SmorodinArt Smorodin
Hi sfdc coolcrm,

You trigger is for the Task object and it will run every time a Task is created or updated. This all makes sense, but you are not checking for related Task on the object.
On Line 7 you run query to find the Milestone__c parent record related to a Task in question, this is good. In the loop following you say 
"for (Task t : Trigger.new)" and in all likelihood this is going to be a single Task (insert or update on just one Task record). Hence every time a particular Task is created/updated which is set to "Complete" the counter variable will return 0 and parent Milestone__c record will be updated. 

What you need to do is to add another SOQL to look for Completed status in all tasks related to parent object from Line 7.

Something along the lines:
List <Task> relatedTasks  = [SELECT Id, Status__c FROM Milestone__c WHERE whatId =: milestn.Id];
for (Task t : relatedTasks){
	if(t.Status != 'Completed')
                {
                  counter++;
                }
}

Hope it all make sense and works for you,

-Art.

All Answers

Art SmorodinArt Smorodin
Hi sfdc coolcrm,

You trigger is for the Task object and it will run every time a Task is created or updated. This all makes sense, but you are not checking for related Task on the object.
On Line 7 you run query to find the Milestone__c parent record related to a Task in question, this is good. In the loop following you say 
"for (Task t : Trigger.new)" and in all likelihood this is going to be a single Task (insert or update on just one Task record). Hence every time a particular Task is created/updated which is set to "Complete" the counter variable will return 0 and parent Milestone__c record will be updated. 

What you need to do is to add another SOQL to look for Completed status in all tasks related to parent object from Line 7.

Something along the lines:
List <Task> relatedTasks  = [SELECT Id, Status__c FROM Milestone__c WHERE whatId =: milestn.Id];
for (Task t : relatedTasks){
	if(t.Status != 'Completed')
                {
                  counter++;
                }
}

Hope it all make sense and works for you,

-Art.
This was selected as the best answer
sfdc coolcrmsfdc coolcrm
Hi Art,
Thank you very much for your help. Issue is resolved after adding the new list on task at line 7.
Thanks