Opportunity Assignment Rules in Salesforce.com via Apex & Imagination

Opportunity Assignment RuleWouldn’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!)

  1. Decide what criteria you want to use to assign Opportunities. For example, maybe you want to assign Opportunities based on their Industry.
  2. 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.
  3. 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.
  4. Create an Apex trigger that fires when an Opportunity is created. This trigger will:
    1. Copy the new Opportunity’s assignment criteria fields (which you identified in step 2, above) into the corresponding Lead fields.
    2. 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.)
    3. 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.)
    4. Determine which user the new Lead Assignment Rule assigned to the new Lead, and change the Opportunity to be owned by that user.
    5. 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.
    6. 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!

 

MJ Kahn, SVP of Technology at OpFocus

about the author

MJ Kahn

At OpFocus, MJ architects and constructs solutions that would impress the builders of the pyramids. She solves technical puzzles that would frustrate daVinci. She leaps tall buildings (like the new Salesforce tower) in a single bound.

Well ok, maybe she doesn’t. But she does help lead an amazing team of smart, talented, and dedicated consultants who do. MJ’s job at OpFocus is provide technical leadership and guidance to OpFocus clients and team members so that, working together, we can create innovative yet practical solutions to real-world business problems within the Salesforce ecosystem.

Prior to OpFocus, MJ built an extensive background in technology and has held a variety of development, consulting, and management positions at companies like Sybase, Vignette, and Honeywell, among others. In these roles, MJ’s focus was helping companies effectively and intelligently use technology to solve business problems. An Apex and Visualforce consultant since mid-2008, MJ has worked with scores of companies to help them understand and utilize platforms like Force.com to solve business issues and also guide them in developing their own AppExchange product offerings.