+ Start a Discussion
ashish jadhav 9ashish jadhav 9 

trigger to update child object?

Hi Guys, I've created 2 custom object with master detail relationship and I want a trigger which update child object when phone number in parent record gets changed, here is my code

trigger UpdateChildPhone on Parent_Dept__c (after insert) {
    Map<id, Parent_Dept__c> MapParent = new map<id, Parent_Dept__c>();
    //checking whether the value already exist or not.
    for(Parent_Dept__c ParentObj : Trigger.New)
    {
        If(Trigger.oldmap.get(ParentObj.id).Dept_Phone__c!= ParentObj.Dept_Phone__c)
        {
            MapParent.put(ParentObj.id, ParentObj);
        }
    }
    //fetching up the data from child with parent ID
    List<Child_Emp__c> ChildObjLst = new list<Child_Emp__c>();
    for(Child_Emp__c child : [SELECT Dept_Phone__c,Id,Parent_Dept__c FROM Child_Emp__c WHERE ID IN : MapParent.keyset()])
    {
      child.Dept_Phone__c= MapParent.get(Parent_Dept__c.id).Dept_Phone__c;
      ChildObjLst.add(child);
    }
        update ChildObjLst;                                           
    }

There is an error on bold colord code, can you please suggest your valuable suggestion please?
Ishwar ShindeIshwar Shinde
Hi Ashish,

What is an error message your are facing? 

And another suggestion, why do you need trigger to peform this operation? As your parent phone number should be displayed on child, then this can be achieved using formula field on child object. Formula - Parent_Dept__c.Dept_Phone__c

Hope it will help!!
ashish jadhav 9ashish jadhav 9

Hi Ishwar, I'm getting below error
Invalid foreign key relationship: Child_Emp__c.Parent_Dept__c

actually I need to get value from map and assign it to the child objects phone number, so is it a right way to retrive value from map?

Ishwar ShindeIshwar Shinde
Hi Ashish,

You can try below code. Changes are highlighted in bold. Let me know if you faced any issue - 

trigger UpdateChildPhone on Parent_Dept__c (after insert) {
    Map<id, Parent_Dept__c> MapParent = new map<id, Parent_Dept__c>();
    //checking whether the value already exist or not.
    for(Parent_Dept__c ParentObj : Trigger.New)
    {
        If(Trigger.oldmap.get(ParentObj.id).Dept_Phone__c!= ParentObj.Dept_Phone__c)
        {
            MapParent.put(ParentObj.id, ParentObj);
        }
    }
    //fetching up the data from child with parent ID
    List<Child_Emp__c> ChildObjLst = new list<Child_Emp__c>();
    for(Child_Emp__c child : [SELECT Dept_Phone__c,Id,Parent_Dept__c FROM Child_Emp__c WHERE Parent_Dept__c IN : MapParent.keyset()])
    {
      child.Dept_Phone__c = MapParent.get(child.Parent_Dept__c).Dept_Phone__c;
      ChildObjLst.add(child);
    }
        update ChildObjLst;                                           
}


I would suggest to use formula field approach and avoid customization. 
Muneendar POC 6Muneendar POC 6
trigger UpdateChildPhone on Parent_Dept__c (after Update) {
    Map<id, Parent_Dept__c> MapParent = new map<id, Parent_Dept__c>();
    List<Child_Emp__c> ChildObjLst = new list<Child_Emp__c>();
    
    //checking whether the value already exist or not.
    for(Parent_Dept__c ParentObj : Trigger.New){
        If(Trigger.oldmap.get(ParentObj.id).Dept_Phone__c!= ParentObj.Dept_Phone__c){
            MapParent.put(ParentObj.id, ParentObj);
        }
    }
    /*
*fetching up the data from child with parent ID
*/
    //Updated the query and 16th line please Check
    for(Child_Emp__c child : [SELECT Dept_Phone__c,Id,Parent_Dept__c FROM Child_Emp__c WHERE Parent_Dept__c IN : MapParent.keyset()]){
        child.Dept_Phone__c= MapParent.get(child.Parent_Dept__c).Dept_Phone__c;
        ChildObjLst.add(child);
    }
    update ChildObjLst;                                           
}

Please Let me know if any issues and select this Answer as best if resolved
Thanks,
Muneendar B

 
Nitin SharmaNitin Sharma
Hello
Use below code as Reference . Just change the name of object and field.
// Trigger on Account object to handle after update action.
trigger AccountTrigger on Account (after update){
    Set<Id> accIdSet = new Set<Id>();
    // Iterating through the input records.
    for(Account acc: Trigger.new){
        if(acc.Phone != Trigger.oldMap.get(acc.Id).Phone) {
            accIdSet.add(acc.Id);
        }
    }
    // Check if Set is empty or not.
    if(!accIdSet.isEmpty()) {
        List<Contact> conList = [Select Id, OtherPhone, AccountId, Account.Phone from Contact where AccountId IN: accIdSet];
        if(conList != null && !conList.isEmpty()) {
            for(Contact con: conList) {
                con.OtherPhone = con.Account.Phone;
            }
            update conList;
        }
    }

}

Thanks,
Nitin Sharma
Ishwar ShindeIshwar Shinde
Hi Ashish,

Is this problem resolved? If yes, please close this query, else share the issue you are facing now?

Thanks and Regards,
Ishwar Shinde