Lead Assignment Rules are really important in your Org if you want to properly manage your sales pipeline. Most companies are using some kind of round-robin where every lead is assigned to a different Sales Rep or they are using some rules based on territory.
When you manually edit a lead, there's a small checkbox, allowing you to assign the owner based on those rules, but sometimes you need to massively create/update/reassign leads.
Here's a really simple script you can use to apply the rules in Apex!
1. Create a Database.DMLOptions object where you can set the assignmentRuleHeader.useDefaultRule property to true. That's the magic property that will tell Salesforce to apply the Assignment Rules.
2. Use a simple SOQL query to get the leads where you want to update the owners.
3. Use the setOptions function on lead. That will make Salesforce use the options we created before when the database operation will be called on that lead.
4. Update the lead collection.
I can easily remember a few scenarios where that simple code would have been useful. I hope it can save you some time too, let me know in the comments if it did!
Visit the Salesforce documentation for more information about the Database.DMLOptions object!
Comments