I'm just curious, is there a method to access an auto-increment number in a newly inserted record without making another SOQL call? Something similar to how MySQL can call up the last insert ID after a query? I'm concerned that my trigger as written is going to run into the governors pretty quickly, so I'm trying to pare it down a bit.

Some background: I'm writing a trigger that is performing a round-robin assignment of leads based on certain criteria, so the correct portion of our sales staff gets assigned certain leads, and I'm using a custom object to keep track of what lead is assigned to whom. I have an auto-incrementing value on the records that I apply the modulus operation on, so I can assign the leads in turn when they come in. If anyone has any better ideas on how to accomplish this, I'm all ears.

  • Have you considered putting the leads into a queue instead, then adding workflow where if they're not taken within 24 hours they'll be auto-assigned by your trigger? It would reduce the number of calls you'd need to make if ppl were taking leads on their own. However, you might want to change the logic to check the number of open leads each was handling if that were the case.
    – crmprogdev
    May 20, 2013 at 15:30
  • 1
    How about pragmaticforce.blogspot.com/2011/07/…
    – Mike Chale
    May 20, 2013 at 16:22
  • @crmprogdev, I'm not really sure how to do that in this instance. The leads are being inserted into our CRM directly from Marketo and not through a web interface of any kind.
    – Lisa
    May 20, 2013 at 17:12
  • @MikeChale your first suggestion looks interesting and I'm going to look into it further. I've already investigated the 2nd, and since we're doing it by criteria (i.e. only some of the leads to only some of the reps), just using custom fields was not a workable option.
    – Lisa
    May 20, 2013 at 17:15

2 Answers 2


I tend to random assignment rather than round robin by generating which member of the team is the target by multiplying Math.random by the team size.

Eg code similar to:

User assignee = mylistOfCandidateUsers.get(Math.random() * teamSize);

This avoids any of that DML and whilst the result won't be predictable it will scale.


I ended up using the solution @mike-chale suggested. http://pragmaticforce.blogspot.com/2011/07/custom-round-robin-lead-assignment.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .