0

I have 4 record types, 3 (Service, Access and Equipment orders) are child, and the parent is (customer Orders)

When a date is updated in one of the child obejcts, I would like that date to be updated in the parents record as well.

E.g: If a order date changes in a order (child) then I would like this date to be displayed in the parent (Customer)

Also as they are all under one brunch object it will require mapping, how will I do this?

This is what I have so far:

trigger updateCaseDate on Case(before update){



Set<Id> setParentId=new Set<Id>();
List<Case> lstCaseUpdate=new List<Case>();

for(Case cs: Trigger.New){
    if(cs.ParentId!=null && cs.Datetime_Required__c!=Trigger.OldMap.get(cs.Id).Datetime_Required__c){
        setParentId.add(cs.ParentId);
    }
}

if(setParentId.ISEMPTY()) return;

Map<Id, Case> mapCaseDetails=new Map<Id, Case>([Select Id, RFS_Date__c,Access_Install_Date__c,Equipment_Install_Date__c from Case where Id IN: setParentId limit 10000]);

for(Case cs: Trigger.New){
    if(cs.ParentId!=null){
        if(Recordtype='Service_Order'){
            mapCaseDetails.get(cs.ParentId).RFS_Date__c=Date.Valueof(cs.Datetime_Required__c);
            lstCaseUpdate.add(mapCaseDetails.get(cs.ParentId));
        }

    }
}


if(lstCaseUpdate.size()>0){
 update lstCaseUpdate;
}
1
  • Is there some reason that you couldn't accomplish this using a lookup relationship and then a formula field? Will there be multiple child records of the same type (i.e. multiple Service order records) associated with the parent case? Jun 22, 2015 at 21:00

1 Answer 1

1

As I understand it you are trying to have the child case update the parent case when the "DateTime Required" field is updated on the child. It is not pretty but this code will pull the field from the child and update the related parent case:

> public class UpdateParent {
>     public static void updateChild(List<Case> newTrigger){
>         Set<Case> caseUpdate = new Set<Case>();
>         for (Case c : newTrigger){
>             if(Trigger.oldMap.get(c.Id).get('Datetime_Required__c') != c.Datetime_Required__c && c.ParentId != NULL){
>                 Case newCase = new Case(
>                   Datetime_Required__c = c.Datetime_Required__c,
>                     Id = c.ParentId                    
>                 );
>                 caseUpdate.add(newCase);
>             }
>         }
>         List<Case> caseList = New List<Case>(caseUpdate);
>         update caseList;
>     } }
1
  • And call the function using UpdateParent.updateChild(Trigger.new); May 23, 2015 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .