Auto-fill Lead Conversion Fields

sugarcrmdevelopers —  September 21, 2011

Editor’s Note: This post comes from the blog of Sugar Partner and Open+ Developer Program member Profiling Solutions. In this post, developer Matt Poer provides some details on a customization he has done to the Lead Conversion process.

Had a partner recently ask how they would go about making a particular change to the Lead Conversion process. Their customer was unhappy having to name the new Opportunity and new Call that the conversion process created. We needed a way to automatically populate these fields. 

From poking around, it looks like the relevant smarty template files (.tpl) don’t reference the typical javascript definition in the view definitions (convertlead.def), so we had to go a less conventional route: override the view class to include some client-side javascript that does the job.

Here’s what I came up with. This script defines a new Opportunity Name, Call Name and Call Date (“next business day”) for the screen. It sleeps for 5 seconds to let the screen load, then, if the field is not already populated with something, it inserts the field values.

The code is commented and should be easy enough to modify. Place it at /custom/modules/Leads/views/view.convertlead.php and it should be registered by the system immediately. It’s upgrade-safe and works with SugarCRM 6.2.2 (CE/Corp/Pro/Ent/Ult), but do double-check things as I haven’t tested in 6.3.0 Beta yet.

Questions or comments are welcome, enter them below!

class CustomViewConvertLead extends ViewConvertLead {
    function display(){
        /* Before the page actually loads, we want to create to scripting to
         * automatically populate certain fields based on the lead's Account
         * We want the Opportunity Name to be the same as the Account's, with
         * "- Opportunity" appended to it. Same for a scheduled call, but we
         * also want to move the call's scheduled time to the next business day
         * To add more fields to this script, simply define some variable like
         * $myField = '';
         * Then populate it using logic or whatever
         * $myField = "$this->bean->last_name";
         * Then add it to the javascript fields in the function
         * PSI_PopulateNewFieldHandler() with the others. The last
         * parameter (true|false) will specify whether or not we want
         * to ignore an existing value in the field, which we had to do for
         * the Call, but most fields you probably won't want to do that.
        $psi_opp_name = '';
        $psi_call_name = '';

        if(empty($this->bean->opportunity_name) && empty($this->bean->opportunity_id) && !empty($this->bean->account_name))
            $psi_opp_name = $this->bean->account_name . " - Opportunity";

            $psi_call_name = $this->bean->account_name . " - Call";

        // Schedule the call for the next business day, assuming a Mon-Fri work week
        $dt = new DateTime();
        switch ($dt->format('D')){
            case "Fri":
                $dt->modify('+3 day');
            case "Sat":
                $dt->modify('+2 days');
                $dt->modify('+1 day');
        $psi_call_date = $dt->format('m/d/Y');

        // A messy way to have done this, but it works
        $javscript = "n";
        $javscript .= "function PSI_PopulateNewField(ElemId,NewValue,IgnoreExisting){n";
        $javscript .= "     var currentElem = YAHOO.util.Dom.get(ElemId);n";
        $javscript .= "     if(currentElem && ((currentElem.value == '') || IgnoreExisting == true)){n";
        $javscript .= "         currentElem.value = NewValue;n";
        $javscript .= "     }n";
        $javscript .= "}n";
        $javscript .= "function PSI_PopulateNewFieldHandler(){n";
        $javscript .= "     PSI_PopulateNewField('Opportunitiesname','$psi_opp_name',false)n";
        $javscript .= "     PSI_PopulateNewField('Callsname','$psi_call_name',false)n";
        $javscript .= "     PSI_PopulateNewField('Callsdate_start_date','$psi_call_date',true)n";
        $javscript .= "}n";
        // give the page 5 seconds to download & display before we attempt to do work on the DOM
        $javscript .= "setTimeout('PSI_PopulateNewFieldHandler()',5000);n";
        $javscript .= "";
        echo $javscript;

        // continue as normal