You need to sign in to do that
Don't have an account?
ashish 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?
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?
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!!
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?
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.
Use below code as Reference . Just change the name of object and field.
Thanks,
Nitin Sharma
Is this problem resolved? If yes, please close this query, else share the issue you are facing now?
Thanks and Regards,
Ishwar Shinde