Salesforce Custom Lead Conversion
Lead conversion in one sense is a pretty simple concept. You define when the lead should be converted, the user clicks the ‘Convert’ button and voila, you have a new Account/Contact/Opportunity that mapped a whole bunch of fields for you automatically. The really cool part with the declarative ( Point-n-Click ) piece of Salesforce is that you can control the mapping of your fields by clicking on the ‘Map Lead Fields’ button on the page that lists the fields for the Lead object.
Great! But what if your business requirements are not that simple?
What if you want to change some values before the lead conversion occurs? What if you want to do different actions during the lead conversion depending on the record type of your lead?
At first glance we could try to do something with a workflow rule and/or a trigger. These are great thoughts as we always want to try to use the declarative power of Salesforce first, but we cannot get in via these methods to modify the lead if the standard ‘Convert’ button has been clicked. Even a concept as simple as saying I want a specific field to be filled during the ‘Convert’ process is kicked off cannot be done. We can grab the resulting Account, Contact and Opportunity in the ‘After Trigger’ of the lead and do whatever we want to those objects there. But our example of trying to do something to the lead before the ‘Convert’ occurs is basically not possible. I was hoping the Before Trigger could help us out, but the actual convert action is causing the update to the Lead so that also does not help. Another example is if you have ‘Person-Accounts’ enabled. If this is true then the company field is no longer required. But maybe if the lead has a certain record type, then you want to make sure the company field gets filled out so that a Business account instead of a Person Account is created. When Person Accounts are enabled the Company field is not longer required and a lead converts to a Person Account if the Company is not filled out.
What should we do?
To make these types of requirements work we have to go back to a standard pattern:
1) A custom button calls a Visualforce page. We could call the new button ‘Convert Special’
2) The ID of the Lead is passed to the Apex Class and the lead is converted using the Database.LeadConvert object
3) The Visualforce page directs onto the resulting Account, Contact or Opportunity depending on your requirements.
That last one is nice. In the typical lead ‘Convert’ scenario you are always redirected onto the Contact object, but since we have a Visualforce page in place we can now direct to wherever we want. You would probably want to remove the standard ‘Convert’ button from your page layouts too.
Another thing that can be nicely done in the Apex class is to default the records types of the Account, Contact and/or Opportunity depending on the values of the lead that was converted. This can really help the user to save time so that the resulting objects are setup automatically with the correct data.
The last thing I learned while working with these conversion requirements is that Notes and Attachments cannot be just transferred to another Account or Contact without writing a loop to do so which will create new objects. The reason for this is that the parentID on the Note and Attachment records cannot be updated. It is only though creating new records and then deleting the old ones that this is possible. I had to work through this because it was a requirement in some cases to delete the resulting Contact object and then copy all of the Notes and Attachments from the Contact up to the Account.
Please let me know if Sundog can help you with any Lead Scoring or Lead Conversion activities in your Salesforce processes!