+ Start a Discussion

Prevent change of ownership when maximum number of records reached


Does anyone know a good way to prevent users from taking ownership of records from a queue when they reach a certain record count?


For example, a user is allowed to pull leads from the queue, but once they own 2,500 leads, they should be unable to take more.


I'm thinking I could create two fields on the user object:

  • User.Max_Lead_Count (Number)
  • User.Current_Lead_Count (Number)

And then create a validation rule preventing ownership when the current lead count is equal to the max lead count.


My question is what kind of apex should I write to populate a lead count field on the user object?


You could do a before update trigger on the Lead object such that any time the lead owner changes from a queue to a person, do an SOQL query for all leads owned by that person, and use the size of the returned list to populate the current lead count value. I imagine you need to fine-tune that calculation since a before trigger won't account for the record(s) that is/are currently being processed by the trigger.