You need to sign in to do that
Don't have an account?
SFDC ADM 7
trigger to update values from multiple fields from child to parent
Hi All,
I am new to trigger. This is my requirement
I have one field in parent object (P1__c). This is long text
I have 4 fileds in child object. (C1__c, C2__c, C3__c, C4__c). All these are text fields
Whenever I updated all child fields, I want to merge these four fields and I want to put it in parent filed.
If I change parent for child recrds, I want to remove that value and I want to update in new parent recod fields.
Please help me. It is urgent.
Thanks in Advance!!
I am new to trigger. This is my requirement
I have one field in parent object (P1__c). This is long text
I have 4 fileds in child object. (C1__c, C2__c, C3__c, C4__c). All these are text fields
Whenever I updated all child fields, I want to merge these four fields and I want to put it in parent filed.
If I change parent for child recrds, I want to remove that value and I want to update in new parent recod fields.
Please help me. It is urgent.
Thanks in Advance!!
What needs to be done in scenario where 1 parent record has more than one child record?
Hi Malni,
Thank you very much for your response.
One parent will have more than one child and maximum 20 child records.
For example, if I enter data in one child record as C1__c=A, C2__c=B, C3__c=C, C4__c=D.
Then in parent object record, P1__c should store as P1__c=ABCD.
My question was, lets say, you have 3 child records for 1 parent Parent 1,
child 1 - C1__c=A1, C2__c=B1, C3__c=C1, C4__c=D1
Child 2 - C1__c=A2, C2__c=B2, C3__c=C2, C4__c=D2
Child 3 - C1__c=A3, C2__c=B3, C3__c=C3, C4__c=D3
What should be the value of Parent.P1__c?
If you have not found solution yet,
Here is the tested code -- This one just updates the parent record for the updated/inserted child record.
trigger CombineText on ChildObj__c (after insert, after update) {
Map<id, string> mapPObj = new Map<Id, String>();
List<ParentObj__c> updParentList = new list<ParentObj__c>();
if (Trigger.isupdate)
{
ChildObj__c oldChObj = new ChildObj__c();
for(ChildObj__c cObj : trigger.new)
{
oldChObj = trigger.oldMap.get(cObj.Id);
If (oldChObj.C1__c <> cObj.C1__c || oldChObj.C2__c <> cObj.C2__c || oldChObj.C3__c <> cObj.C3__c || oldChObj.C4__c <> cObj.C4__c)
{
mapPObj.put(cObj.ParentobjId__c, cObj.C1__c + cObj.C2__c + cObj.C3__c+ cObj.C4__c);
}
}
}
else if (Trigger.isinsert)
{
List<ParentObj__c> updParentList = new list<ParentObj__c>();
for(ChildObj__c cObj : trigger.new)
{
mapPObj.put(cObj.ParentobjId__c, cObj.C1__c + cObj.C2__c + cObj.C3__c+ cObj.C4__c);
}
update updParentList;
}
if (mapPObj.size() > 0)
{
updParentList = [Select Id, P1__c from parentobj__c where id in :mapPObj.keySet()];
for (parentObj__c pObj : updParentList)
{
pobj.P1__c = mapPobj.get(pObj.Id);
}
update updParentList;
}
}
Hope this helps!