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

Roll Up summary field for lookup relatioship

My scenario is to have a Roll up summary field for Look up relationship object.
Please let me know the work around.

Suraj GharatSuraj Gharat

Salesforce supoorts Roll Up summary fields only with Master-Detail relationship. Yet if you need to have it without Master-Detail, you will have to implement its functionality by your own. For instance you may use Apex trigger on child object to compute custom roll up summary value on parent record.
Hi Suraj,

Can you plz share a sample of that code for reference.
Suraj GharatSuraj Gharat
Ok, you me refer below code. This code updates custom roll up summary field "Roll_Up_Summary_Count__c" on parent object, by the number of children records. Similarly you can implement your solution for MAX, AVG and MIN etc.
Trigger SomeTriggerName On Child_Object__c (After Insert){
	Set<Id> setParentIds=new Set<Id>();
	List<Parent_Object__c> lstParentsToUpdate=new List<Parent_Object__c>();
	for(AggregateResult result:[Select parent_relation_field__c,count(id) From Child_Object__c WHERE parent_relation_field__c IN :setParentIds GROUP BY parent_relation_field__c LIMIT 2000]){
		Parent_Object__c parent=new Parent_Object__c();
	update lstParentsToUpdate;

Flauradel ConsumidoFlauradel Consumido
I came accross your code and was trying this, however, I keep receiving Error: Compile Error: Invalid field: 'Commission__c' at line 6 column 32

I have a Custom object and set up a lookup from Opportunity to the custom object. What I would like is to get the sum of the Amount of all the opportunities attached to the custom object. 

I need to roll up:
From Object: Opportunity
Field: Amount

Where to roll up: 
Object: Commission
Field: Total_Amount__c

Below is the code:  
trigger CommissionRollUp on Commission__c (After Insert){
    Set<Id> setOpportunityIds=new Set<Id>();
    List<Opportunity> lstOpportunityToUpdate=new List<Opportunity>();
    for(AggregateResult result:[Select Commission__c,count(id) From Commission__c WHERE Commission__c IN :setOpportunityIds GROUP BY Commission__c LIMIT 2000]){
        Opportunity__r parent=new Opportunity__r();
    update lstOpportunityToUpdate;

Suraj GharatSuraj Gharat
  • The solution needs to have an Apex trigger on child object which, in your case, is Opportunity not Commission__c.
  • The trigger should run the logic for every possible event that may affect your roll-up value, for instance insert, delete and may be update event too. Below code only handles "insert" event, you may need to add code for other required events.
trigger CommissionRollUp on Opportunity (After Insert){
	Set<Id> setCommissionIds = new Set<Id>();
	for(Opportunity opp :
	List<Commission__c> lstCommissionsToBeUpdated = new List<Commission__c>();
	for(AggregateResult result:[	Select 	Commission__c,
									From 	Opportunity 
									WHERE 	Commission__c IN :setCommissionIds 
									GROUP BY Commission__c LIMIT 2000]){
		Commission__c com = new Commission__c();
		com.Id = result.get('Commission__c');
		com.Total_Amount__c = (Integer)result.get('expr0');
	update lstCommissionsToBeUpdated;

Hope this helps.