0

This question already has an answer here:

Why salesforce does not support Roll-up Summary on Account for Contact as it is only supporting one standard object which is Opportunity ?

I have checked for Roll-up Summary ( https://trailhead.salesforce.com/en/point_click_business_logic/roll_up_summary_fields ) and salesforce says that its mandatory to have Master-Detail relationship between objects then only you can have Roll-up Summary field on parent object.

According to above, how is Roll-up Summary on Account is supported for Opportunity object ?

Please check these screenshots: enter image description here

marked as duplicate by Eric, Tushar Sharma, Vigneshwaran G, Ratan Paul, superfell Dec 13 '16 at 20:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    An opportunity must have an account. A contact does not. So opp is MD and contact is lookup – Eric Dec 12 '16 at 7:02
  • @Eric I know that for RollUp you need to have MD, but Account and Opportunity are not MD still Salesforce allows to create RollUp. Is their any limitation, why we cannot create RollUp on Account for Contacts similar to Opportunities. – tejas033 Dec 13 '16 at 7:07
  • 1
    It is what it is.....When it comes to standard fields you do what SF says you can do even if it seems to not follow rules for custom fields. For custom fields you follow rules for custom fields. No need to make is that difficult. – Eric Dec 13 '16 at 7:09
2

This is because the relationship between Contact and Account is a lookup relationship not a Master Detail. Rollup summaries are only available when a M:D relationship exists.

There is a free tool for declarative rollup summaried between non MD relationships created by Andrew Fawcett that is available at AppExchange or GitHub

0

Relationship between Account and Opportunity and Account and Contact answers my question why do Salesforce does not support Roll-up Summary between Account and Contact ?

Specially comment: "Many organizations find it useful to be able to roll up opportunity stats to an account (e.g. total sales, pipeline, etc), while only a few organizations would find it useful to roll up contacts. That's not to say that you can't write your own code to perform a roll up yourself, but simply that there hasn't been sufficient interest or priority to implement the feature."

My work around to achieve "Total Number of Contacts" on Account page will be by implementing Trigger on Contact object.
Found this link: Displaying Total Number of Contacts for each Account in Salesforce

Trigger code:

trigger ContactsOnAccount on Contact (after insert, after delete,after undelete,after update) {
    Set<Id> aId = new Set<Id>();

    if(Trigger.isInsert || Trigger.isUndelete){
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];

        for(Account a : acc){
            a.No_of_Contacts_in_SFDC__c=con.size();
        }update acc;
    }

    if(Trigger.isDelete){
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];

        for(Account a : acc){
            a.No_of_Contacts_in_SFDC__c=con.size();
        }update acc;
    }

    if(Trigger.isUpdate){
        Set<Id> OldAId = new Set<Id>(); 
        for(Contact opp : Trigger.new){
            if(opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c)
                aId.add(opp.AccountId);
                OldAId.add(Trigger.oldMap.get(opp.id).AccountId);
        }
        if(!aId.isEmpty()){
            //for new Accounts
            List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
            //For New Account Contacts
            List<Contact> con = [select id from contact where AccountId in :aId];

            /*This is For Old Contacts Count*/

            //for Old Accounts
            List<Account> Oldacc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:OldAId];

            //For Old Account Contacts
            List<Contact> OldCon = [select id from contact where AccountId in :OldAId];

            //For New Accounts
            for(Account a : acc){
                a.No_of_Contacts_in_SFDC__c=con.size();


            }update acc;

            //For Old Accounts
            for(Account a : Oldacc){
                a.No_of_Contacts_in_SFDC__c=OldCon.size();

            }update Oldacc;
        }
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.