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
Vinay_guptaVinay_gupta 

Rollup summary on lookup Relationship

-----Rollup Summary of Lookup Relationship-------------------------

Hi All,

I want to create a Rollup field in Account Object with Custom Object called 'Contact Partner'.

'Contact Partner' Object is maintaining a lookup relationship with Account Object. 

I want to fill the Rollup field with name 'Total Revenue' in Account object, which should tell the total amount of Revenue generated by each Contact Partner.

Each 'Contact Partner' would be maintaining the field called 'Revenue Generated'.

For Ex

Account A has three Contact Partner(CP) and each CP is generating revenue of 10$,20$ & 30$. Hence the field 'Total Revenue' in Account object should automatically fill  with '60$'

 

Can anyone help me on this along with the Code?, Actually I'm new on the trigger.

WEN JIEWEN JIE
You can create al roll-up field on Account object, and then choose "Select Roll-Up Type" with corresponding contact partner field.
v varaprasadv varaprasad
Hi varun,

Please check once below code below code will count number of contacts on account object.

http://salesforceprasad.blogspot.in/2017/12/roll-up-summary-functionality-through.html#gpluscomments




For your requirement please change code like below.

public integer double totalAmount;

      for(account acc : lstAccs){
            system.debug('==acc.contacts.size()=='+acc.contacts.size());
            for(contact con = acc.contacts){
            totalAmount = con.amount__c;
        }
          acc.totalAmountofcon__c = totalAmount;
          updLstOfAccs.add(acc);
        }
        
         if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }

Hope it helps you.
if it helps you please mark it as best answer.

Please let me know incase of any other help.

Thanks
Varaprasad

 
v varaprasadv varaprasad
  for(contact con : acc.contacts){
            totalAmount = con.amount__c;
        }
Vinay_guptaVinay_gupta

Hi Varaprasad,
Thanks for your quick response.

I have written the below class by changing the code which you have shared. But i am getting the below error.  The error looks like that i haven't written the correct SOQL. Can you please help me here?


FYR.....
Parent Object -->Customer_Outlet__c
Child Object-->Contact_Partner__c
Relationshop is look up

1)Customer_Outlet__c has field 'Total_CP_Salary__c '
2)Contact_Partner__c has field 'Salary__c '

My objective is to fill 'Total_CP_Salary__c' field automatically, which is the summation of 'Salary__c ' of each  Contact_Partner__c under Customer_Outlet__c .

Can you please help me to achieve this?
 





User-added image





Regards,
Varun





 

v varaprasadv varaprasad
Hi Varun,

Please try once below code :
 
public class SampleRollupSummary {   
    public static void rollupContacts(list<Contact_Partner__c> lstOfconts){
        system.debug('==lstOfconts== : '+lstOfconts);
        set<id> accIds = new set<id>();
        list<Customer_Outlet__c> updLstOfAccs = new list<Customer_Outlet__c>();       
        public double  totalAmount = 0;
        for(Contact_Partner__c con : lstOfconts){
            accIds.add(con.Customer_Assigned__c);
        }
        system.debug('==accIds==:'+accIds);
       list<Customer_Outlet__c> lstAccs = [select id,Total_CP_Salary__c,(select id,Salary__c from Contact_Partner__r) from Customer_Outlet__c where id in : accIds];
       
        for(Customer_Outlet__c acc : lstAccs){
		    
		   for(Contact_Partner__c cpr : acc.Contact_Partner__r){
		       totalAmount = cpr.Salary__c;
		   }
		   acc.Total_CP_Salary__c = totalAmount; 
           updLstOfAccs.add(acc);
        }
        if(updLstOfAccs.size() > 0){
            update updLstOfAccs;
        }
       
       
    }
   
}

Hope it will help you.


Thanks
Varaprasad 

 
Vinay_guptaVinay_gupta
Hi Varaprasad,

I am still getting the below error.


User-added image


Code Written:-

public class SampleRollupSummary {  
    
    public static void rollupContacts(list<Contact_Partner__c> lstOfconts){
        
        system.debug('==lstOfconts== : '+lstOfconts);
        
        set<id> accIds = new set<id>();
        
        list<Customer_Outlet__c> updLstOfAccs = new list<Customer_Outlet__c>();      
        
        public double  totalAmount = 0;
        
        for(Contact_Partner__c con : lstOfconts){
            
            accIds.add(con.Customer_Assigned__c);
            
        }
        
        system.debug('==accIds==:'+accIds);
        
        list<Customer_Outlet__c> lstAccs = [select id,Total_CP_Salary__c,(select id,Salary__c from Contact_Partner__r) from Customer_Outlet__c where id in : accIds];
        
        
        
        for(Customer_Outlet__c acc : lstAccs){
            
            
            
            for(Contact_Partner__c cpr : acc.Contact_Partner__r){
                
                totalAmount = cpr.Salary__c;
                
            }
            
            acc.Total_CP_Salary__c = totalAmount;
            
            updLstOfAccs.add(acc);
            
        }
        
        if(updLstOfAccs.size() > 0){
            
            update updLstOfAccs;
            
        }
        
        
        
        
        
    }
    
    
    
}



v varaprasadv varaprasad
Hi varun,

Please check once child object relationship name of Contact_Partner__r in the child object.
Then use child relationship name on a query.

Example like below : 

http://raydehler.com/cloud/clod/salesforce-soql-subquery-and-custom-objects-with-apex.html\

Then use like below query : 
Select Name, (Select Name from ObjectDetails__r) From ObjectMaster__c

Hope this helps.

Thanks
Varaprasad
 
v varaprasadv varaprasad
list<Customer_Outlet__c> lstAccs = [selectid,Total_CP_Salary__c,(select id,Salary__c from Contact_Partners__r) from Customer_Outlet__c whereid in : accIds]; 
use above query i think yourchildrelationshipname is  : Contact_Partners
Vinay_guptaVinay_gupta
Hi Varaprasad,

I am able to save the class now. The mistake is shown below:

Instead of 'Contact_Partners__r' we were putting 'Contact_Partner__r'.



     list<Customer_Outlet__c> lstAccs = [select id,Total_CP_Salary__c,(select id,Salary__c from Contact_Partners__r) from Customer_Outlet__c where id in : accIds];



I observed that the output is not accurate. Please have a look.



User-added image
v varaprasadv varaprasad
public double  totalAmount = 0; 

use below line instead of above line.
public integer totalAmount = 0;



Thanks
Varaprasad

 
v varaprasadv varaprasad
Hi Varun,


Please add below lineinsted of  totalAmount = cpr.Salary__c
totalAmount =  totalAmount  + cpr.Salary__c;

Thanks
Varaprasad
Vinay_guptaVinay_gupta
Thank you so much for your help, Varaprasad.

It worked.!!!

 
Vinay_guptaVinay_gupta
Hi Varaprasad and All,

I need one help and Suggestion. Can this be created using Map<id,list<Contact>> instead of using the only List?

If yes, can you please share the Code?

Regards,
Varun
Vinay_guptaVinay_gupta
Hi All,

Can anyone please help me here?

If I want to perform this operation using Map. Can we do it?

If yes, please can you help me with a code?

Regards,
Varun