• sfdc coolcrm
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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;}

}
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;}

}