Opportunity Assignment Rules in Salesforce.com via Apex & Imagination
Wouldn’t it be great if we could assign Opportunities to users in the same way we assign Leads and Cases, using an easily-configurable Assignment Rule? Unfortunately, “out of the box” Salesforce doesn’t allow this. However, if you think out of the box, you can make it happen.
The trick is to use a little imagination. Pretend that your Opportunity isn’t an Opportunity at all – pretend it’s a Lead. If it were a Lead, you could write a Lead Assignment Rule to assign the Lead to the correct person, right? So write that Lead Assignment Rule. Create a Lead that mimics your Opportunity and use the Lead Assignment Rule to assign the Lead to the correct person. Then assign your Opportunity to that person, and delete your Lead. When you’re done, your Opportunity has been re-assigned, using your Lead Assignment Rule as a guide!
Here are the gory details. (Spoiler alert – some Apex knowledge will help here!)
- Decide what criteria you want to use to assign Opportunities. For example, maybe you want to assign Opportunities based on their Industry.
- Determine which Opportunity fields are involved in your assignment criteria (in our example, Industry), and, if necessary, create corresponding fields for the Lead object. These fields don’t have to be on the Lead page layout; they just have to be defined for the Lead object.
- Create a Lead Assignment Rule that implements your assignment criteria as if you were assigning Leads, rather than Opportunities. Don’t make this Rule active; it doesn’t need to be active for our purposes.
- Create an Apex trigger that fires when an Opportunity is created. This trigger will:
- Copy the new Opportunity’s assignment criteria fields (which you identified in step 2, above) into the corresponding Lead fields.
- Set a Database Savepoint. (Setting a savepoint tells Salesforce to remember the current state of the database. In just a sec, we’ll make some changes to the database and examine the results, and then we’ll tell Salesforce to roll back (restore) the database to the state it was in before we made those changes.)
- Insert the Lead, telling the database to apply your new Lead Assignment Rule to determine who the owner of the new Lead should be. (If you’re an Apex groupie, this means using a DMLOption with the Id of your new Lead Assignment Rule.)
- Determine which user the new Lead Assignment Rule assigned to the new Lead, and change the Opportunity to be owned by that user.
- Roll back the database to the state it was in when you set the Savepoint. This prevents the new Lead from being created, which is fine. Once we’ve determined who the new Lead would have been assigned to, we don’t need the Lead to be saved in the database.
- Allow the creation of the Opportunity to continue. The Opportunity will be created, and will be assigned to the user you specified in step 4.4.
Clearly, this involves writing some Apex code, so it’s not for the faint of heart. The good news is, once you’ve got the Apex implemented properly, if you decide to change your assignment criteria, you can do so without having to modify the Apex code – you just change your new Lead Assignment rule, and the next time the trigger runs, your Opportunity will be assigned to the right person!