function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
trigger restrictContactCreation on Contact(before insert){ Set<Id> accountIds = new Set<Id>(); Map<Id,Integer> mapAccountIdWithContactCount = new Map<Id,Integer>(); for(Contact con : trigger.new){ if(con.AccountId != null){ accountIds.add(con.AccountId); } } if(!accountIds.isEmpty()){ for(Account acc: [SELECT Id,(SELECT Id FROM Contacts) FROM Account WHERE Id IN : accountIds]){ Integer conCount = 0; if(acc.Contacts.size() > 0){ conCount = acc.Contacts.size(); } mapAccountIdWithContactCount.put(acc.id,conCount); } } for(Contact con : trigger.new){ if(con.AccountId != null && mapAccountIdWithContactCount != null && mapAccountIdWithContactCount.get(con.AccountId) != null){ if(mapAccountIdWithContactCount.get(con.AccountId) >= 3){ con.addError('You cannot create more than three contacts for an account'); } } } }
Try below code
Mark this as "SOLVED" if your query is answered.
Thanks & Regards,
Bhanu Mahesh Gadi