8 contacts as maximun in a specific account - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Luis Fernando Vega QuinteroLuis Fernando Vega Quintero 

8 contacts as maximun in a specific account

Hi Everyone,
I'm try to make a practice and i need the contacts of a specific account that don´t exceed the amount of 8, i mean that account can´t have 9 contacts just the maximun of 8 contacts
I don´t know where to start.
Greetings
Best Answer chosen by Luis Fernando Vega Quintero
Usman AliUsman Ali
Hi Luis, 
Thanks for sharing code, I just created a trigger in my Dev org, and I am to save that code. could you please try again or use delveoper console to create trigger.
User-added image
trigger ContactTrigger on Contact (after insert, before Insert, before update, after update) 
{
    if ((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter) 
    {
        Set<Id> setofAccountId = new Set<Id>();
        for(Contact objCon : Trigger.new)
        {
            setofAccountId.add(objCon.accountid);
        }

        //Get all account contact related values
        Map<Id,Account> mapofAccIdToContacts = new Map<Id,Account>([SELECT Id,(Select Id From Contacts) From Account Where Id IN : setofAccountId]);
        
        for(Contact objCon : Trigger.new)
        {
            if(mapofAccIdToContacts.containskey(objCon.accountID) && mapofAccIdToContacts.get(objCon.accountID).Contacts.size() > 8)
                objCon.addError('cannot have more than 8 contacts per account');
        }
    }
}

 

All Answers

Luis Fernando Vega QuinteroLuis Fernando Vega Quintero
Everything i try to get the total of the contacts in the specific account si wrong, so please can you give me a hand on this. thanks a lot
Mira ShahMira Shah
Please see the link below of similiar issue
https://salesforce.stackexchange.com/questions/257764/contact-trigger-to-limit-an-account-to-not-have-more-than-2-contacts
https://developer.salesforce.com/forums/?id=9060G000000I1cyQAC
 
Luis Fernando Vega QuinteroLuis Fernando Vega Quintero
hi i´m actually getting this error, there is no other way like validation rule to make this work?
the error shows that is missing (;) but i can not find where.
User-added image
Thanks.
Usman AliUsman Ali
On line # 5 there is a ; semi colon missing at the end. 
Kapil BatraKapil Batra
Hi Luis,

If you want to display the “Total Number of Contacts” for each “Account” in Salesforce on the Account page you can not use the Roll Up Summary field. The Roll Up Summary fields on contact are not supported on Accounts. We have used a Trigger on the contact object to  find the sum of the number of contacts which are there in an Account. The trigger basically updates the Account custom field “Number_of_contacts__c” with the number of contacts that Account has. Since this trigger is on the Standard object it can be used in any Salesforce.com ORG by creating this custom field on Account.

Check the complete article below : 
https://www.dhruvsoft.com/blog/displaying-total-number-of-contacts-for-each-account-in-salesforce/
Luis Fernando Vega QuinteroLuis Fernando Vega Quintero
Hi Usman,
I writte the missing character, but still i´m getting errors, i send the pic,
User-added image
 
Usman AliUsman Ali
Hi Luis, 
Could you please paste your code here?
Luis Fernando Vega QuinteroLuis Fernando Vega Quintero
here i paste it, my objetive is to get only 8 contact per account, i mean the maximun of contacts should be 8.

trigger ContactTrigger on Contact (after insert, before Insert, before update, after update) {
    if ((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter) {
        Set<Id> setofAccountId = new Set<Id>();
        for(Contact objCon : trigger.new)
            setofAccountId.add(objCon.accountid);
        //Get all account contact related values
        Map<Id,Account> mapofAccIdToContacts = new Map<Id,Account>([Select Id,(Select Id From Contacts) From Account Where Id IN : setofAccountId]);
        for(Contact objCon : trigger.new){
            if(mapofAccIdToContacts.containskey(objCon.accountID) && mapofAccIdToContacts.get(objCon.accountID).Contacts.size() > 8)
                objCon.addError('cannot have more than 8 contacts per account');
        }
    }
}
Usman AliUsman Ali
Hi Luis, 
Thanks for sharing code, I just created a trigger in my Dev org, and I am to save that code. could you please try again or use delveoper console to create trigger.
User-added image
trigger ContactTrigger on Contact (after insert, before Insert, before update, after update) 
{
    if ((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter) 
    {
        Set<Id> setofAccountId = new Set<Id>();
        for(Contact objCon : Trigger.new)
        {
            setofAccountId.add(objCon.accountid);
        }

        //Get all account contact related values
        Map<Id,Account> mapofAccIdToContacts = new Map<Id,Account>([SELECT Id,(Select Id From Contacts) From Account Where Id IN : setofAccountId]);
        
        for(Contact objCon : Trigger.new)
        {
            if(mapofAccIdToContacts.containskey(objCon.accountID) && mapofAccIdToContacts.get(objCon.accountID).Contacts.size() > 8)
                objCon.addError('cannot have more than 8 contacts per account');
        }
    }
}

 
This was selected as the best answer
Luis Fernando Vega QuinteroLuis Fernando Vega Quintero
Hi Usman,
thanks a lot!, i was on the account object, i realized that i should be on the contact objects - Trigger
Now it don´t show errors.
Again thanks.