function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC ADM 7SFDC 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!!
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
SFDC Adm 7
What needs to be done in scenario where 1 parent record has more than one child record?
SFDC ADM 7SFDC ADM 7

Hi Malni,

Thank you very much for your response.
One parent will have more than one child and maximum 20 child records.

Malni Chandrasekaran 2Malni Chandrasekaran 2
In such case, what data should be added in P1__c. Please give me those details
SFDC ADM 7SFDC ADM 7
The data which I enter in child object record.
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.
Malni Chandrasekaran 2Malni Chandrasekaran 2
SFDC,
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!