Silly Dev Trick: Running Lead Assignment Rules on New Leads

Another posting that resulted from a lot of great examples on the web but none that were actually bulked and tested.

So the business case is “Run the default lead assignment rules when leads are created”. Seems easy but when you look at the documentation, forums, and blog postings it is all none bulked examples with all kinds of answers for how to bulk. The issue I kept hitting was this error:

“DML statement cannot operate on or Trigger.old”

Wait what? Works fine if you just update the record but if you added it to a list blamo. Someone mentioned future class as an option but that involves all kinds of overhead for catching when the queue is full and managing that. So noodling the error message I decided to give a “what if sObject?” and that works like a charm. Calling the class on After Insert.

Below is an example class that is tested and works. Created a checkbox to manage resubmissions/looping but other than that it’s pretty straight forward:

Leave a Reply

Your email address will not be published. Required fields are marked *