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
Pranav T'iwaryPranav T'iwary 

Account and Contact has the lookup relationship now i want the rollup type of filed in the Account page where i can show the count of my all contacts. I cannot change the relationship from Lookup to master- Detail.

Account and Contact has the lookup relationship. Now i want the rollup kind type of field in the Account page where i can show the count of my all contacts. I cannot change the relationship from Lookup to master- Detail.

How i will achieve this in the Account page in lookup relationship ?
Best Answer chosen by Pranav T'iwary
Harshit Garg 6Harshit Garg 6

HI Pranav,

Create roll-up summary fields on:
1.Any custom object that is on the master side of a master-detail relationship
2.Any standard object that is on the master side of a master-detail relationship with a custom object
3.Opportunities using the values of opportunity products related to the opportunity
4.Accounts using the values of related opportunities
5.Campaigns using campaign member status or the values of campaign member custom fields


So cannot make the roll up the summary with contacts.

You can achieve this through the trigger. Below is the link.
Please follow the link steps.

You could also use something like this which looks to completely solve the issue:
https://ericsantiago.com/2009/12/11/contactscases-rollup-for-accounts-in-salesforce/

Please choose my answer as the best answer.if that's  helps you.

Thanks,
Harshit Garg
harshitgarg2591@gmail.com

 

All Answers

LBKLBK
Hi Pranav,

You need a AFTER INSERT , AFTER UPDATE trigger on contact to achieve this.

Refer the following posts for more information.

http://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/
https://developer.salesforce.com/forums/?id=906F0000000g0q1IAA

Hope this helps.
Harshit Garg 6Harshit Garg 6

HI Pranav,

Create roll-up summary fields on:
1.Any custom object that is on the master side of a master-detail relationship
2.Any standard object that is on the master side of a master-detail relationship with a custom object
3.Opportunities using the values of opportunity products related to the opportunity
4.Accounts using the values of related opportunities
5.Campaigns using campaign member status or the values of campaign member custom fields


So cannot make the roll up the summary with contacts.

You can achieve this through the trigger. Below is the link.
Please follow the link steps.

You could also use something like this which looks to completely solve the issue:
https://ericsantiago.com/2009/12/11/contactscases-rollup-for-accounts-in-salesforce/

Please choose my answer as the best answer.if that's  helps you.

Thanks,
Harshit Garg
harshitgarg2591@gmail.com

 

This was selected as the best answer
Pranav ChitransPranav Chitrans
Hi Pranav,

Please find the below code and and update according to senario,in this trigger i used custom object, you just need to replace the same with your desired object which you want to use 
Apex Trigger :
trigger CountChildForEachParent on Child__c (after insert, after update, after delete, after undelete)  
{
    HandlerForChildToCountRecords handler = new HandlerForChildToCountRecords ();
    handler.run();
}
Apex Class :
public class HandlerForChildToCountRecords 
{    
    public void run()
    {
        if(trigger.isInsert || trigger.isUpdate && trigger.isAfter)
        {   
            afterInsertMethod(trigger.new);
        }
        
        if(trigger.isDelete  && trigger.isAfter)
        {   
            afterDeleteMethod(trigger.new,trigger.old);
        }
        
        if(trigger.isUpdate && trigger.isAfter)
        {   
            afterUpdateMethod( trigger.new, trigger.OldMap);
        }
    }
    
    private void afterInsertMethod(list<child__c>triggerNew)
    {
        //commonMethod(triggerNew,null);
        set<id> setOfId  = new set<id>();           
        for(child__c objChildNew : triggerNew)
        {
            if(objChildNew.Parent_name__c != null)
            {
                setOfId.add(objChildNew.Parent_name__c);
                system.debug('*****setOfId'+setOfId);
            }
        }
        if(setOfId.size() > 0)
        {
            //calling commom method to perofrm the operation, so as to reduce the line of code
            commonMethod(setOfId);
        }
    }
    
    Public void afterUpdateMethod( list<sobject> triggerNew, map<id,sobject> triggerOldMap)
    {
        set<id> setOfId = new set<id>();    
        
        list<child__c> lstChild = (list<child__c>) triggerNew;
        map<id, child__c> mapChild = (map<id, child__c>) triggerOldMap;
        for(child__c childObj : lstChild)
        {
            if(childObj.Parent_name__c != null && mapChild.get(childObj.id).Parent_name__c != null && childObj.Parent_name__c != mapChild.get(childObj.id).Parent_name__c)
            {
                setOfId.add(childObj.Parent_name__c);
                setOfId.add(mapChild.get(childObj.id).Parent_name__c);
            }
        }
        
        if(setOfId.size() > 0)
        {
            //calling commom method to perofrm the operation, so as to reduce the line of code
            commonMethod(setOfId);
        }
    } 
    
    private void afterDeleteMethod(list<child__c>triggerNew,list<child__c>triggerOld)
    {
        set<id> setOfId = new set<id>();           
        for(child__c objChildNew : triggerOld)
        {
            if(objChildNew.Parent_name__c != null)
            {
                setOfId.add(objChildNew.Parent_name__c);
                system.debug('*****setOfId'+setOfId);
            }
        }
        if(setOfId.size() > 0)
        {
            //calling commom method to perofrm the operation, so as to reduce the line of code
            commonMethod(setOfId);
        }
    }
    
    //Common Method to perform insertion/updation/deletion to query records
    private void commonMethod(set<id> setOfId)
    {
        try
        {           
            list<Parent__c> lstOfParent = new list<Parent__c>([select id,name,ContactsCount__c ,(select id,name from Child__r)
                                                                                                         from 
                                                                                                        Parent__c 
                                                                                                    where id IN:setOfId]);
            system.debug('*****lstOfParent'+lstOfParent);
            for(Parent__c objPrnt : lstOfParent)
            {
                if(objPrnt.ContactsCount__c != objPrnt.Child__r.size())
                {
                    objPrnt.ContactsCount__c = objPrnt.Child__r.size();
                }
            }
            update lstOfParent;
        }
        
        catch(DmlException e)
        {
            system.debug('The following exception has occurred: ' + e.getMessage()); 
        }  
    }          
}

This trigger also work on deletion of record and reparitng the parent of child, in both cases it will the child count.
Hope this help!

Thanks
Pranav