Doing More with Lead Conversion

sugarcrmdevelopers —  August 27, 2012

A recent project requirement forced me to utilize some the Lead Conversion process to ensure some additional data was copied from the original Lead record to the newly-created records. By default, Sugar handles this nicely if the fields are named the exact same name. Unfortunately this isn’t always the case with relationships (particularly many-to-many) and doesn’t really allow for the creation of custom records (although that can be done in many ways via Studio as well). For the heavy stuff, we need to get into code of the Convert Lead view.

Technically, we shouldn’t be doing business-logic in the view, but this is the easiest and most-upgrade-safe way of accomplishing the goal. We’re going to override the functionality that displays the end-results of a Lead Conversion, i.e. the screen that says “Create Contact: Joe Smith and Account Joe’s Co.

The $beans parameter to this method contains the original Lead record as well as the newly-created records. You can parse this array for the records created, or append a new object to it if you’re creating new records, and it will be displayed appropriately. The following examples does the former, parsing $beans and setting up to handle the records based on what sort of object they are (Opportunity, Account or Contact):

/custom/modules/Leads/views/view.convertlead.php

<?php
require_once('modules/Leads/views/view.convertlead.php');
class CustomViewConvertLead extends ViewConvertLead {
	function displaySaveResults($beans){

		foreach($beans as $bean){
			switch($bean->module_dir){
				case "Contacts":
						break;
				case "Opportunities":
						break;
				case "Accounts":
						break;
			}

		}

		parent::displaySaveResults($beans);
	}
}