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
About MeAbout Me 

Update parent field using Batch and Trigger

Hi All, I have a scenario where ParentObject Field A needs to be updated based on the ChildObject Start and End Dates. Difference between the start date and End dates are to be calculated and displayed on the Field A(parent object field)
Scenario 1) After Update trigger: Trigger should update ParentObject Field A when ChildObject Start and End dates arent Blank which does not with the code I have below. 
Scenario 2) Batch Process to update the same field (FieldA of parent object)when my ChildObject Start is not blank and End date is Blank(different between the start date and date.today)
In my code, I am setting the field values to zero every time I run the batch and Trigger, so losing the old values which I do not want instead I want the new value appended to the old value. If I comment on the logic of making the field zero from Batch class then when I run the batch multiple times the value is doubling instead should only consider if the date. today is changed. Not sure how to handle this scenario
//batchProcess
global class  UpdateBatch implements Database.Batchable<sObject>, schedulable {
   
   global Id id_AgreementCustom = null; 
    List<ParentObj> aGCList = new List<ParentObj>();
    
    //parametrized constructor for passing specific record to process
    global UpdateBatch(Id agcuId){
        System.debug('@Developer --> UpdateBatch -->:' + agcuId);
        id_AgreementCustom = agcuId;
    }
    
    //parametrized constructor
    global UpdateBatch() {
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
         System.debug('@Developer --> DepartmentReviewUpdateBatch -->:' + id_AgreementCustom);
        String sSOQL = 'Select Id, Name,FieldA, ';
        sSOQL += ' (Select id,name,Value,, ';
        sSOQL += ' startdate ';
        sSOQL += ' From Parent.ChildRelation__r ';
        sSOQL += ' Where enddate = NULL ) ';
        sSOQL += '  ';
        sSOQL += ' From ParentObj ';
        
        if(id_AgreementCustom != null){
            sSOQL += ' Where  Id =\'' + id_AgreementCustom + '\'';    
        }
        if (test.isRunningTest()){
            sSOQL += ' limit 5 ';
        }
        return Database.getQueryLocator(sSOQL);
    }  
    global void execute(Database.BatchableContext bc, List<ParentObj> scope) {
        for(ParentObj aGC : scope){
          //  FieldA          = 0;
            for(ChidlObj dR : Parent.ChildRelation__r){
                
				BusinessDaysUtilityClass bDays= new BusinessDaysUtilityClass();
             
  			 datetime startDate = ChildObj.StartDate;
                datetime endDate   = date.today(); 
          
                Integer updateWorkingDays = bDays.getNoOfBusinessDaysBetweenDates(startDate.date(), endDate.date());
                
				if(startdate !=NULL){   
                    if(value == 'Salesforce'){
                    
                             FieldA += updateWorkingDays;                  
                    }     
            }
            aGCList.add(aGC);    
        }
        Map<Id, parentObj> aGMapToUpdate = new Map<Id,parentObj>();
        aGMapToUpdate.putAll(aGCList);
        if(aGMapToUpdate.size()>0){    
            update  aGMapToUpdate.values();  
        }       
    }
    global void execute(SchedulableContext sc) {}
    global void finish(Database.BatchableContext bc) { }
}

//triggerHandler
public class TriggerHandler {  
    public static void DaysCalculation(list<ChildObj> dRList){     
        Set<Id> ctoIDS = new Set<Id>();
        List<ParentObj> aGCList = new List<ParentObj>();    
        for(ChildObj dr : dRList){
            ctoIDS.add(dr.Parent__c);
        }
        for(ParentOBj aGC : [select id, name,FieldA(select id,name,Value,StartDate,EndDate
                                                                           from Child__r) from ParentA where Id =:ctoIDS]){
                                                                               
                                                                            FieldA         = 0;
                                                                               for(ChildObj d : aGC.Child__r){
                                                                                   BusinessDaysUtilityClass bDays= new BusinessDaysUtilityClass();
                                                                                   datetime startDate = cObj.startdate;
                                                                                   datetime endDate   = cObj.enddate;
                                                                                   system.debug('@developer-->startDate'+startDate);
                                                                                   system.debug('@developer-->endDate'+endDate);
                                                                                   
                                                                                   if(startdate != NULL && enddate != NULL){
                                                                                       Integer updateWorkingDays = bDays.getNoOfBusinessDaysBetweenDates(startDate.date(), endDate.date());
                                                                                       sy
                                                                                       if(Value == 'Salesforce'){                                               
                                                                                           FieldA += updateWorkingDays;               
                                                                                       }     
                                                                               }
                                                                               aGCList.add(aGC);
                                                                           }
        Map<Id, ParentObj> aGMapToUpdate = new Map<Id,ParentObj>();
        aGMapToUpdate.putAll(aGCList);
        if(aGMapToUpdate.size()>0){    
            update  aGMapToUpdate.values();  
        }         
}

as the same parentObj Field A is updating via 2 processes and every time a process updates the field A values should be added instead of doubling or losing the old value.The code snippet below.