You need to sign in to do that
Don't have an account?

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!!
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
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
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
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
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;
}