Wednesday 10 May 2017

Update child to Grand parent

Updating Child to grandparent

1. Entitlement - Grand Parent
2. Case - Parent
3. Task - Child for case
4. Task -  Grand Child for Entitlement
5. Getting the Task object have the field called "Duration".
6. Getting the duration value and updating the Entitlement.
7. Entitlement was related to case & we are fetching the case related task.

        public void setRollupsummaryent(List<task> lstask)
        {
             if(Trigger.isUpdate)
              {
                Set<Id> caseId = new Set<Id>();
                for(Task ts1 : lstask)
                {
                    caseId.add(ts1.whatId);
                }
               
                Set<Id> entId = new Set<Id>();
                for(Case cs : [select Entitlementid, (Select Id,Duration__c from Tasks) from case where id in : caseId])
                {
                    entId.add(cs.EntitlementId);  
                }
               
                Map<Id,Decimal> entWithDuration = new Map<Id,Decimal>();
                for(Case cs : [select Entitlementid, (Select Id,Duration__c from Tasks) from case where Entitlementid in : entId ])
                {  
                   
                    Decimal duration = 0;
                    for(Task tk: cs.tasks){
                        if(tk.Duration__c != null){
                        duration += tk.Duration__c;
                        System.debug('&&&&&&&&&&&&' + duration);
                    }
                    }
                    if(entWithDuration.containsKey(cs.EntitlementId)){
                        Decimal temp = entWithDuration.get(cs.EntitlementId) + duration;
                        entWithDuration.put(cs.entitlementId, duration);
                    }else{
                        entWithDuration.put(cs.entitlementId, duration);
                    }
                }
               
                List<Entitlement> updateList = new List<Entitlement>();
                for(Entitlement etm: [Select Id,Used_hours__c from Entitlement where Id In:entWithDuration.keySet()]){
                   
                    etm.Used_hours__c = entWithDuration.get(etm.Id);
                    updateList.add(etm);
                }
               
                if(updateList.size() > 0){
                    update updateList;
                }
               
              }
        }

No comments:

Post a Comment

Batch Apex

1. What are transaction limits in apex? Total number of SOQL queries issued1 - 100 Total number of records retrieved by SOQL queries - 50...