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
Bryan REVELANT 2Bryan REVELANT 2 

Trigger on Child to update the parent

Hello

I have a trigger that I have a on a child record to update the parent if the conditions are met.. The trigger however isnt writting anythign to the parent.

If child has rejected write rejected.
If the record is submit and  not all approved then submit
else approved.

trigger ADRUserApproval on ADRUser__c (after update) {

list<id> ADRSubmittedList = new list<id>();
    list<ADR__c> ADRUserList = new list<ADR__c>();
   
    for(ADRUser__c lstADRTrigger : trigger.new){
        if(lstADRTrigger.Status__c == 'Submit' || lstADRTrigger.Status__c == 'Approved' || lstADRTrigger.Status__c == 'Rejected')
        {
        
       ADRSubmittedList.add(lstADRTrigger.id);
        }//*** End of if(lstADRTrigger.Next_Step__c...
    }//*** End of for(ADR__c lstADRTrigger : trigger.new){
 
    for(ADR__c lstADRTrigger : [ Select id, Apex_Status__c from ADR__c
    where id in :ADRSubmittedList] ){

       for(ADRUser__c lstADRUser : trigger.new){
       
         
       
       
        if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved' || lstADRUser.Status__c == 'Rejected')
        {
        
         // if(lstADRUser.Status__c == 'Submit' && lstADRUser.Status__c == 'Approved' && lstADRUser.Status__c == 'Rejected'){
       
       lstADRTrigger.Apex_Status__c = 'Rejected';
      
    if(lstADRUser.Status__c == 'Submit' || lstADRUser.Status__c == 'Approved'){
            lstADRTrigger.Apex_Status__c = 'Submit';
         }
           else lstADRTrigger.Apex_Status__c = 'Approved';
           //  Site_Audit_Results__c FindAnswersVar = new Site_Audit_Results__c();
        ADR__c ADCUpdates = new ADR__c();
      
          ADCUpdates.Apex_Status__c = lstADRTrigger.Apex_Status__c;

        System.debug('lstADRTrigger ' + lstADRTrigger);
    
       
      ADRUserList.add(ADCUpdates);
     
          System.debug('ADCUpdates ' + ADCUpdates);
      update ADRUserList;
     


   
//}
}
}
}
}
Scott McClungScott McClung
Hi Bryan,
Looks like the reason you're not updating the parent records is because your SOQL query isn't return any.  You're attempting to query the ADR__c object using  the ID values in ADRSubmittedList, but that list has been populated with ID's from the ADRUser__c.  Because of that you'll always get 0 results.
To fix that you'll need to update this line...
ADRSubmittedList.add(lstADRTrigger.id);
...to refer to the parent Id field on the ADRUser__c object.  Something like...
ADRSubmittedList.add(lstADRTrigger.Parent_Id_Field__c);
I don't see any other reference to that parent Id field in your code, so I'm not sure what it's called.

You've got a few other issues in your example code that will probably crop up once some records are returned and it starts executing more of the code.  The biggest thing you'll want to avoid in the future is putting your update call inside of a loop.  You'll run into your DML limit in no time like that. :)

Here is an example of how you could simplify/bulkify your code.
trigger ADRUserApproval on ADRUser__c (after update) {

  //This map will hold the final approval status for each ADR__c record
  Map<Id, String> mapApprovals  = new Map<Id, String>(); //The map key is Id of ADR__c record
 
  for(ADRUser__c lstADRTrigger : trigger.new) 
  {
    if(lstADRTrigger.Status__c == 'Submit' || lstADRTrigger.Status__c == 'Approved' || lstADRTrigger.Status__c == 'Rejected')
    {
      //Populate the map with the results of the ADRUser__c records in the trigger
      if(!mapApprovals.containsKey(lstADRTrigger.Parent_Record_Id__c))
        mapApprovals.put(lstADRTrigger.Parent_Record_Id__c, 'Approved'); //Default to approved

      if(lstADRTrigger.Status__c == 'Rejected')
        mapApprovals.put(lstADRTrigger.Parent_Record_Id__c, 'Rejected'); //Overwrite with rejected

      if(lstADRTrigger.Status__c == 'Submit' && mapApprovals.get(lstADRTrigger.Parent_Record_Id__c) != 'Rejected')
        mapApprovals.put(lstADRTrigger.Parent_Record_Id__c, 'Submit'); //If it isn't already rejected, then not all approved so set to submit
    }
  }

  List<ADR__c> lstADR = [SELECT Id, Apex_Status__c FROM ADR__c WHERE Id IN :mapApprovals.keySet()];
  for(ADR__c objADR : lstADR) 
  {
    if(mapApprovals.containsKey(objADR.Id))
    {
      objADR.Apex_Status__c = mapApprovals.get(objADR.Id);
    }
  }
  update lstADR;
}
I've used a map to associate your business logic of the ADRUser__c status field to each ADR__c record.  This makes it easier to update the ADR__c records with the results.

To use this code, you'll need to replace 'Parent_Record_Id__c with the correct parent id field name from ADRUser__c.

Hope that helps! :)