Create Custom Workflow Process in Dynamics CRM to Qualify Leads
In the latest Tales from the Trenches, Jesse shares his expereinces with custom workflows in Dynamics CRM 2016.
If you’ve dug around in the SDK, you may have found out by now that the Lead Qualification process can be adjusted to create only one or two out of Account, Contact, and Opportunity records, but only in custom code. The default system behavior in CRM when pressing the “Qualify” button is to create all three records, and you’ll find many discussions on workarounds online involving custom Actions, scripts and plugins. However, here are some easier tricks you may not have learned or may never become common knowledge.
1. Ribbon Workbench (or otherwise editing XML) can allow you to add a new button with new behavior
The interface for this tool may be daunting, but for those less interesting in manually editing Solution XML, this will allow you to add a new Ribbon Button onto the Lead entity, and hide the original OOB button. It installs as a Solution and has good documentation to get you started.
I also recommend installing ProcessJS. By including this as a Web Resource on the Lead Form, you can call a Workflow with only one line of code:
https://processjs.codeplex.com/
Afterward, you will be doing only the following:
- Make a new Javascript Web Resource that simply calls the Process.js method to run a Workflow
- Make a new Workflow and make sure you get its GUID to put into the above Javascript call
- Tie a new Ribbon Button into the new Javascript call using Ribbon Workbench
- Finish building out the Workflow
2. Virtually all of the Qualification logic can be replicated in a Workflow
Let’s discuss what Qualification actually does when hitting the OOB button:
- Create records with Mapped attributes, both OOB and custom
- Set the Originating Lead for all new Records
- Make sure each Record is tied to the other (Account to Opportunity, Contact to Account, etc)
- Change the Status of the Lead
- Move the User from the Lead page to the newly created Opportunity (if applicable)
All of this can be done in an On-Demand Workflow, assuming you do the main operations in the correct Order:
- Create Account (if applicable)
- Create Contact (if applicable) and connect it to the Account
- Create Opportunity and connect the Contact and Account
- Set the Lead’s Qualifying Opportunity to the new Opportunity
- Do a Status Change on Lead to change it to Qualified
Note that you may need to have multiple branches to handle different conditions in the Workflow, but you’ll still want to follow the above order in each.
3. A Real-time Workflow that performs a Status change will also provoke navigation to the next page
This is the big undocumented “secret”. If you run a Real-Time workflow that forces a Status change on the Lead to Qualified while you are on the Lead Form, and the Opportunity has already been created and set to the Qualifying Opportunity, the system will automatically send the User’s browser to the created Opportunity! This appears to mimic “enough” of what CRM is expecting behind the scenes on the default Qualify button to trigger the same logic in motion.
CRM has been known to present developers with many problems related to order-of-operations in Javascript, and being unable to properly predict or control how CRM will run a Save or Refresh on a web page. However, the above method saves the record during Qualification without relying on an explicit Save Event, so the logic executed by the Workflow is straightforward and avoids any further complications or moving pieces.
(Note that all of this was tested only in CRM 2016)