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 GuestSFDC Guest 

Apex trigger to update child record from another child record of same parent object

Hi All, can u please help me with below trigger. Thanks in advance.

Req: Parent object have two child objects.They are child1, child2. If child1 is inserted then child2 Description field should be updated with child1 Description.

Objects: ParentObj
Child1 object ==> fields Name, Description
Child2 object ==> fields Name, Description
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger UpdateChild on Child1__c (before insert) {
    
    List<Id> parentIds = new List<Id>();
    for (Child1__c c : Trigger.new){
        if (c.Parent__c != null){
            parentIds.add(c.Parent__c); //Parent API name in child
        }
    }
    List <Child2__c> cList = [SELECT Id, Parent__c FROM Child2__c WHERE Parent__c IN :parentIds];
    
    if (cList.size() > 0){
        for(Child1__c c1 : trigger.new){
            for (Child2__c c2 : cList){
                c2.Description__c = c1.Description__c;
            }
        }
        UPDATE cList;	
        
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi,

Please refer through below code it works fine and as per you requirement:

Trigger---->
trigger Copy_Description on child1__c (before insert) 
{
    Set<Id> parentId_Set = New Set<Id>();
    for (child1__c ch1 : Trigger.new)
    {
        if (ch1.ParentId_c != null)
        {
            parentId_Set.add(ch1.ParentId_c);
        }
    }
    List <child2__c> child2_List = [SELECT Id,ParentId_c FROM child2__c where ParentId_c in :parentId_Set];
    if (child2_List.size() > 0)
    {
        for(child1__c ch1 : trigger.new)
        {
            for (child2__c ch2 : child2_List)
            {
                if(ch1.ParentId_c == ch2.ParentId_c)
                {
                    ch2.Description__c = ch1.Description__c;
                }
            }
        }
        update child2_List;    
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi,

I have gone through your problem. To create the parent object has two child objects. They are child1, child2. If child1 is inserted then child2
 The description field should be updated with child1 Description.
 Please go through code given below:-


trigger===> 


trigger Copy_Description on child1__c (before insert) 
{
   if(trigger.isbefore && trigger.isInsert){
       handlerclass.main(trigger.new);
   }
}

Handler===>

public class handlerclass {
    public static void main(List<child1__c>   chld1list){
        Set<Id> parentId_Set = New Set<Id>();
        for (child1__c ch1 : chld1list)
        {
            if (ch1.ParentId_c != null)
            {
                parentId_Set.add(ch1.ParentId_c);
            }
        }
        List <child2__c> child2_List = [SELECT Id,ParentId_c FROM child2__c where ParentId_c in :parentId_Set];
        if (child2_List.size() > 0)
        {
            for(child1__c ch1 : chld1list)
            {
                for (child2__c ch2 : child2_List)
                {
                    if(ch1.ParentId_c == ch2.ParentId_c)
                    {
                        ch2.Description__c = ch1.Description__c;
                    }
                }
            }
            update child2_List;    
        }
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com