You need to sign in to do that
Don't have an account?
![shrey.tyagi88@tcs.com shrey.tyagi88@tcs.com](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Calculate summary field on Look up (Master Record), using trigger , Please Help!!!
Hi All
I have 2 custom objects A__C , B__c . Object B looks upto Object A.
Now B has a field say "Revenue__c". A__c has a field "Total Revenue". I wanna write a trigger on B to update Total Revenue__c on A , upon addition,deletion or update of B record. Please help me with the outline of that triggger.
Thanks
What?! Soql queries in for loops? Guys, this is APEX development 101!
Do not put SOQL queries in for loops!
This is the proper batchable way to do this... It will work for you:
Trust me. The other solutions will break under pressure. Mine will not.
Simply copy past this thing, I just did it in my own org.
Replace my fields an objects with yours and you are done.
All Answers
For trigger, use the following code:
trigger updateTotalRevenue on B(after update, after delete){
for(B__c ob:Trigger.new)
{
List<A__c> temp = [SELECT Total_Revenue__c FROM A__c WHERE Id=:ob.lookupFieldName LIMIT 1];
//Do all the calculations
temp[0].Total_Revenue__c = 10;
update temp[0];
}
}
Hi,
try this
trigger revenueUpdation on B__c(after insert,after update, after delete){
if(Trigger.isDelete()){
for(B__c bObj : Trigger.old){
A__c aObj = new A__c;
aObj = [SELECT Total_Revenue__c FROM A__c WHERE Id =: bObj.lookupFieldName ];
aObj.Total_Revenue__c += bObj.Revenue__c;
upsert aObj;
}
}
else{
for(B__c bObj : Trigger.new){
A__c aObj = new A__c;
aObj = [SELECT Total_Revenue__c FROM A__c WHERE Id =: bObj.lookupFieldName ];
aObj.Total_Revenue__c += bObj.Revenue__c;
upsert aObj;
}
}
}
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Thanks
Select A__c a,SUM(Revenue__c) total from B__c where A__c =: b.A__c group By A__c
Now create a new instance on A__c using alias a and update total revenue
What?! Soql queries in for loops? Guys, this is APEX development 101!
Do not put SOQL queries in for loops!
This is the proper batchable way to do this... It will work for you:
Trust me. The other solutions will break under pressure. Mine will not.
Simply copy past this thing, I just did it in my own org.
Replace my fields an objects with yours and you are done.
Thanks A ton JayNic. You saved me a hell lot of time!!!!
i.e there are three fields on b object debt, revenue, expenses. And I wanna map them to 3 different firleds of A object i.e total debt, total revenue and total expenses.. please help
Hi Jay Nic,
There is also something wrong with line
most likely you have empty values in your TotalRevenue__c field. You should enforce that a number is entered even if it is zero
Got it Jay, Your code worked like a charm!!!!
glad to hear it!
If you need to augment this to roll up additional fields, then you can still use the same principles...
near the original map, create another map eg:
And for each if statement, simply add the additional other field using the same form eg:
Then in the final if statement at the bottom, you just need to make sure that it takes in to consideration the new maps, eg:
And you're still only using one soql query!