How to Convert a Lead In Use By a Time-Based Workflow in Salesforce

How to Convert a Lead in Salesforce that is in Use by a Workflow Rule Have you ever tried to convert a Lead, only to get the “Unable to convert lead that is in use by workflow” message? Frustrating, isn’t it?

This message appears if the Lead you want to convert is waiting on the Time-Based Workflow queue, waiting for some event to occur. Salesforce prevents you from converting these Leads until they are removed from the queue. Your options are few:

  • Tell your user to edit the Lead to change whatever value(s) caused the record to be placed on the queue. This should cause the Lead to be removed from the workflow queue, so the user can try again to delete it. Of course, that’s a lot of work to ask your users to do, just to convert a Lead.
  • Tell your users to let you know when they get this message. You can go to the queue (Setup | Monitoring | Time-Based Workflow, find the record on the queue, and delete it. Then tell the user to try again. But do you really want to do that every time a user wants to convert a Lead that’s on the queue?
  • Remove the standard Convert button from the Lead page. Replace it with a custom button that uses JavaScript to call an Apex method that (1) edits the Lead to change the value that caused the record to be placed on the queue, and (2) redirects to the default Convert page. To do this, you have to write the Apex code, plus related unit tests, develop in a sandbox, deploy to Production…. Again, that’s a lot of work!

What you really need is an approach that can be automated so you or your users don’t have perform extra steps, and that doesn’t involve Apex. Wouldn’t it be great if you could just override the standard Convert button?

You can!

The solution involves overriding the standard Convert button, so that it sends the user to a Visualforce page instead. The page, which will be displayed for only a second or less, will check a new checkbox field on your Lead, then redirect to the standard Lead Convert page. If you change your workflow rule’s criteria to exclude any Leads that have this new checkbox checked, then by the time the standard Lead Convert page is displayed, the Lead will no longer be on the workflow queue, and you’ll be able to convert it without any problem.

Here’s what you need to do:

1. Add a new checkbox field

Create a checkbox field named Cancel Workflow to the Lead object. By default, the checkbox is unchecked. The field doesn’t need to appear on the page layout.

2. Edit Your Workflow Rule

Change your workflow rule to add “Cancel Workflow equals False” to the criteria. In other words, if the new checkbox becomes checked, the Lead will no longer meet the workflow criteria.

3. Create a Visualforce page like this:


<apex:page standardController="Lead" > 
<apex:form> 
  <div style="visibility:hidden;"> 
    <apex:inputField value="{!Lead.Cancel_Workflow__c}" id="cancelWorkflow" style="visibility:hidden; "/> 
  </div> 
  <apex:actionFunction name="quickSave" action="{!quickSave}" oncomplete="standardConvert();"/> 
  <apex:actionFunction name="standardConvert" 
    action="{!URLFOR($Action.Lead.Convert, lead.id, [retURL=$CurrentPage.parameters.retURL], true)}" />
  <script language="JavaScript"> 
    var previousOnload = window.onload; 
    window.onload = function() { 
      if (previousOnload) previousOnload(); 
      fixLead(); 
    } 
    function fixLead() { 
      var elemCancelWorkflow = document.getElementById('{!$Component.cancelWorkflow}'); 
      elemCancelWorkflow.checked = true; 
      quickSave(); 
    } 
  </script> 
</apex:form> 
</apex:page>
 

4. Override Lead Object’s Standard Convert Action

Override the Lead object’s standard Convert action with the new Visualforce page. (Use Setup | Customize | Leads | Buttons and Links, then click Edit next to the Convert link to get to the override page.)

With this in place, when you click the Lead’s Convert button, you’ll see a brief flicker as this page is loaded and does its work, but the page will then redirect to the standard Lead Convert page. By the time you get there, the Lead will have been removed from the workflow queue, and you’ll be free to convert the Lead.

Try it, and let us know how it works for you!

Something to Consider

There’s only one issue with this solution. If you decide not to Convert the Lead and instead click Cancel on the Convert page, the Lead will remain unconverted, but its Cancel Workflow field will remain checked, preventing the Lead from being placed on the time-based workflow queue. You could develop a Scheduled Apex process to detect unconverted Leads whose Cancel Workflow field is checked, and then uncheck them, but that involves developing Apex, which we’ve otherwise avoided in this solution. If you’ve got any ideas, please post them here!

Photo credit: Simon_sees

 

Meet the OpFocus Team at Dreamforce 2011 This Summer

That’s right! The OpFocus team will be at Dreamforce to soak in all the information that Salesforce.com has to offer and meet with old and new friends alike. If you are a customer, a friend or a fan of OpFocus, please let us know if you will be attending Dreamforce. We would love to see you there!



im-attending-dreamforce



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.