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

How can we use rollup summary in lookup relationship ?

Biswojeet Ray 11Biswojeet Ray 11

Hi Subodh Kumar,


We can not use rollup in lookup relationship. If you need roll-up functionality in lookup. 

Create a field to show the rollup field value.

Here is the sample code

Here I have created a field "Total_Contact__c" in the account, to show total num of related contacts.

trigger accountTrigger in Account(Before Update) {
    set<Id> accountIds = new set<Id>();
    Map<Id, Account> accountMap = new Map<Id, Account>();
    for (Account acc : Trigger.New) {
        accountMap.put(acc.Id, acc);
    List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN: accountIds];
    List<Account> toupdate = new List<Account>();
    for (Id ids : accountIds) {
        for (Contact Con : contacts) {
        integer count = 0;
            if (Con.AccountId == ids) {
        //Here you can put the field name on which you need to update total number of contacts
        accountMap.get(ids).Total_Contact__c = count,
    Update toupdate;



Please try it.


I hope it helps you.

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,

Raj VakatiRaj Vakati
You can do it .. You can do it using the below
  1. Apex Trigger 
  2. Flows 
  3. Apex Declaritive rollup summer package