You need to sign in to do that
Don't have an account?
SFDC 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
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
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.
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
Please refer through below code it works fine and as per you requirement:
Trigger---->
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
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