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
S_BatmanS_Batman 

Limiting Number of Accounts per User

I am trying to limit the number of Accounts per user that have the custom field "Protected" checked off.  I created the following trigger and tested it out on sandbox but it doesn't seem to be working.

User-added image
Best Answer chosen by S_Batman
Amit Chaudhary 8Amit Chaudhary 8
You need to use below line in your trigger:-
accs.addError('You are your limit of account');

Just replace the System.debug() with above like.
Let us know if this will help you

Thanks
Amit Chaudhary
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
You need to use below line in your trigger:-
accs.addError('You are your limit of account');

Just replace the System.debug() with above like.
Let us know if this will help you

Thanks
Amit Chaudhary
 
This was selected as the best answer
SamarSSamarS
If you are checking for "Protected" checked off then you may need to check condition as 
IF(accs.protected__c == False) instead of True.
Then count existing accounts for that user where "Protected" is checked off and if it is >99 display error if limit is 100.
 
Secondly, Amit rightly mentioned you need to use addError function here to display error.

thanks,
Samar
S_BatmanS_Batman
Thanks Gentlemen!

Is there a way to limit number of Accounts per user without creating a custom field? How will the code differ then ?