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
Scotty ForceScotty Force 

Trigger between two objects

Hello, Have two custom objects Obj1 and Obj2, there is a lookup relationship between them. Obj1 has a field 'Quantity'. Looking for help on trigger, to pass quantity values to Obj2 object (Field: Amount). Want to achieve this using Trigger and not process builder. Any help? Thanks!!
Best Answer chosen by Scotty Force
Arpit Jain7Arpit Jain7

If you want to update samr record or you don't need record ID in such case we generaly use before insert/Update.

trigger obj1Trigger(before insert,before update){
    set<obj2__c>obj2Set = new set<obj2__c);
    list<obj2__c>obj2List = new lit<obj2__c);
    //Loop to traverse the record updated/Inserted
    for(obj1__c obj1:trigger.new)
    {
           //Condition to check parent field is not null and also quantity field is not blank
           if(obj1__c.obj2__c!=null and obj1.Quantity!=null){
                 //New instance for parent object and set related values.
                 obj2__c obj2 = new obj2__c;
                 obj2.Id = obj1.obj2__c;
                 obj2.Amount__c = obj1.Quantity__c;
                 obj2Set.add(obj2);
           }
    }
    //Add the parent records to be updated in a list
    obj2List.addAll(obj2Set);
    //Update the list if it is not blank.
    if(obj2List.size()>0)
    update obj2List;
}

Hope this might hep !!
Thanks
Arpit

All Answers

Arpit Jain7Arpit Jain7
Hello Scotty,

Though this requirement can be achieved by process builder but as you want it with trigger only, You can try below sample code
Assuming that Obj1 is child here
trigger obj1Trigger(before insert,before update){
    set<obj2__c>obj2Set = new set<obj2__c);
    list<obj2__c>obj2List = new lit<obj2__c);
    for(obj1__c obj1:trigger.new)
    {
           if(obj1__c.obj2__c!=null and obj1.Quantity!=null){
                 obj2__c obj2 = new obj2__c;
                 obj2.Id = obj1.obj2__c;
                 obj2.Amount__c = obj1.Quantity__c;
                 obj2Set.add(obj2);
           }
    }
    obj2List.addAll(obj2Set);
    update obj2List;
}

I have just written basic trigger code here. You can modify it further as per your requirement. In case Obj1 is parent you need to chnge your trigger logic to query all related child data and then update that iformation.

Hope this helps !!

Thanks
Arpit
 
Arpit Jain7Arpit Jain7
Hello Scotty,

I hope above solution helped you. If it does please mark it as Best Answer, so that it can help others also with the same problem.

Thanks
​Arpit
Scotty ForceScotty Force
Apologize it took me, so long to respond. Can you please add some comments in the code, so it becomes easy for me to understand. Thanks!!
Arpit Jain7Arpit Jain7

If you want to update samr record or you don't need record ID in such case we generaly use before insert/Update.

trigger obj1Trigger(before insert,before update){
    set<obj2__c>obj2Set = new set<obj2__c);
    list<obj2__c>obj2List = new lit<obj2__c);
    //Loop to traverse the record updated/Inserted
    for(obj1__c obj1:trigger.new)
    {
           //Condition to check parent field is not null and also quantity field is not blank
           if(obj1__c.obj2__c!=null and obj1.Quantity!=null){
                 //New instance for parent object and set related values.
                 obj2__c obj2 = new obj2__c;
                 obj2.Id = obj1.obj2__c;
                 obj2.Amount__c = obj1.Quantity__c;
                 obj2Set.add(obj2);
           }
    }
    //Add the parent records to be updated in a list
    obj2List.addAll(obj2Set);
    //Update the list if it is not blank.
    if(obj2List.size()>0)
    update obj2List;
}

Hope this might hep !!
Thanks
Arpit

This was selected as the best answer
Scotty ForceScotty Force
Hi Arpit, Thank you, the solution works fine.

trigger Obj1Trigger on Obj1__c (before insert, before update) 
{
    // Obj1 (Child) and Obj2 (Parent) in this lookup relationship
    set<obj2__c>obj2Set = new set<obj2__c>();
    list<obj2__c>obj2List = new list<obj2__c>();
    
    //Loop to traverse the record updated/Inserted
    for(obj1__c obj1:trigger.new)
    {
           //Condition to check parent field is not null and also quantity field is not blank
           if(obj1__c.obj2__c != null && obj1.Quantity__c != null)
           {
                 //New instance for parent object and set related values.
                 obj2__c obj2 = new obj2__c();
                 obj2.Id = obj1.obj2__c;
                 obj2.Amount__c = obj1.Quantity__c;
                 obj2Set.add(obj2);
           }
           
    }
    //Add the parent records to be updated in a list
    obj2List.addAll(obj2Set);

    //Update the list if it is not blank.
    if(obj2List.size() >0)
    update obj2List;

}