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
cpo87cpo87 

How to limit number of accounts a user owns?

I am trying to determine the best way to limit the number of accounts a given user owns.  In my case it is accounts with and attribute of "Protected".  I would like them to get an error when they go above 100 protected accounts.  I figured the best way to do this was by writing a trigger that queries the db and if the count() query is greater than 100 throw the error.

 

Is that the right way to do this?  It is beginning to look like I can't throw an error with apex, do I need to use apex and visualforce to acheive this?  Here is my apex so far.

 

 

trigger AccountTop100Trigger on Account (before insert, before update) { Account accs = Trigger.new[0]; If(accs.Protected__c == true){ Integer accounts = [SELECT COUNT()FROM Account WHERE Account.ownerId =: userInfo.getUserId()]; system.debug(accounts); //Output an error if the owner goes over 100 accounts If(accounts > 100){ system.debug('You are over your limit of 100 protected accounts, this account cannot be marked as protected until your remove another protected account.'); } } }

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

That sounds like a reasonable way to do it.

 

You can throw an error from Apex - e.g. you could throw an exception at that point.

 

A better way would be to add an error to the objects that the trigger is working on.   E.g.

 

accs.addError('You are over your limit...');

 

That way, the error will be displayed to the user in a sensible fashion regardless of how the account is being added.

 

As an aside, your trigger is only coded to check the first element, so a bulk update from the dataloader would break your model.  It would be better to extract the count of current accounts, compare that to the number in the trigger.new list, and add errors to all those that would take the count over 100.  That way, if a user had 75 and tried to add 30, the first 25 would succeed.

All Answers

bob_buzzardbob_buzzard

That sounds like a reasonable way to do it.

 

You can throw an error from Apex - e.g. you could throw an exception at that point.

 

A better way would be to add an error to the objects that the trigger is working on.   E.g.

 

accs.addError('You are over your limit...');

 

That way, the error will be displayed to the user in a sensible fashion regardless of how the account is being added.

 

As an aside, your trigger is only coded to check the first element, so a bulk update from the dataloader would break your model.  It would be better to extract the count of current accounts, compare that to the number in the trigger.new list, and add errors to all those that would take the count over 100.  That way, if a user had 75 and tried to add 30, the first 25 would succeed.

This was selected as the best answer
cpo87cpo87
Thanks Bob!