Overriding ViewConvertLead

By default, when converting a lead to account, Sugar retains the assigned user associated with the original lead record, however if the lead is initially assigned to a group user of 'Sales', it would be helpful to have the lead record that is currently being converted to change the assigned user to the current user automatically, whether it's empty or not, so we don't have to rely on users to manually ensure they change the assignee to themselves.

I have a fairly low level understanding that in order to make this happen, I would need to override view.convertlead.php by changing:

if (empty($bean->assigned_user_id))
                {
                    $bean->assigned_user_id = $lead->assigned_user_id;
                }

to:

if (empty($bean->assigned_user_id))
                {
                    $bean->assigned_user_id = $current_user->id;
                }

by creating ./custom/modules/<module>/views/view.convertlead.php with the below:

<?php

require_once('modules/<module>/views/view.convertlead.php');

class CustomViewConvertLead extends ViewConvertLead
{
    function display()
    {
        echo 'This is my addition to the DetailView<br>';

        //call parent display method
        parent::display();
    }
}

however, I'm not sure how to apply the change within this file to make it override. Any advice?

Thanks in advance.

  • Hi Jake Ashley,

    If you are using the sugar latest commercial edition, you can do it by overriding the LeadConvert class available in modules/Leads/LeadConvert.php. Look out for the method name "setAssignedForModulesToLeads" and update $this->lead->assigned_user_id to $GLOBALS['current_user']->id.

    Also, please note that you need to override the convertLead method of LeadConvertApi class as well inside modules/Leads/clients/base/api/LeadConvertApi.php to use your custom LeadConvert class instead of using the standard one.

    In short, you need to override LeadConvert and LeadConvertApi classes in the custom path and you are good to go.

    Let us know if this helps or you need more explanation.

    Regards.

  • Thanks hats

    I want to make sure I am doing this properly. This is my stab at it if you don't mind taking a look.

    Is the preferable method of doing this to copy the core function from the core sugar files or is there another way? Does the system just see that the function has been replaced via the custom directory and ignore that function in the core file?

    ./custom/modules/Leads/CustomLeadConvert.php

    <?php

    require_once 'modules/Leads/LeadConvert.php';

    class CustomLeadConvert extends LeadConvert
    {
        /**
         * Sets the assigned user to the current user
         * @return null
         */

        protected function setAssignedForModulesToLeads($moduleDef)
        {
            $moduleName = $moduleDef['module'];
            if (!empty($this->lead)) {
                if (!empty($this->modules[$moduleName]->assigned_user_id)) {
                    $this->modules[$moduleName]->assigned_user_id = $GLOBALS['current_user']->id;
                }
            }
        }
    }

    ./custom/modules/Leads/clients/base/api/CustomLeadConvertApi.php

    <?php

    require_once 'modules/Leads/clients/base/api/LeadConvertApi.php';

    class CustomLeadConvertApi extends LeadConvertApi
    {
       /**
         * This method handles the /Lead/:id/convert REST endpoint
         *
         * @param ServiceBase $api The API class of the request, used in cases where the API changes how the fields are pulled from the args array.
         * @param array $args The arguments array passed in from the API
         * @return array Worksheet data entries
         * @throws SugarApiExceptionNotAuthorized
         */

        public function convertLead(ServiceBase $api, array $args)
        {
            $customLeadConvert = new CustomLeadConvert($args['leadId']);
            $modules = $this->loadModules($api, $customLeadConvert->getAvailableModules(), $args['modules']);

            $transferActivitiesModules =
                empty($args['transfer_activities_modules']) ? array() : $args['transfer_activities_modules'];
            $transferActivitiesAction =
                empty($args['transfer_activities_action']) ? '' : $args['transfer_activities_action'];

            $modules = $customLeadConvert->convertLead($modules, $transferActivitiesAction, $transferActivitiesModules);

            return array (
                'modules' => $this->formatBeans($api, $args, $modules)
            );
        }
    }
  • Hi Jake Ashley,

    Everything looks fine except you also need to override the API endpoint registration so that system should call your custom API file instead of the standard one.

    <?php

    require_once 'modules/Leads/clients/base/api/LeadConvertApi.php';

    class CustomLeadConvertApi extends LeadConvertApi
    {
        public function registerApiRest()
        {
            //Extend with extraScore property
            $api= array (
                'convertLead' => array(
                    'reqType' => 'POST',
                    'path' => array('Leads', '?', 'convert'),
                    'pathVars' => array('','leadId',''),
                    'method' => 'convertLead',
                    'extraScore' => 1, //this will help calling my custom endpoint
                    'shortHelp' => 'Custom convert Lead endpoint',
                    'longHelp' => 'modules/Leads/clients/base/api/help/LeadConvertApi.html',
                ),
            );
            return $api;
        }

        /**
         * This method handles the /Lead/:id/convert REST endpoint
         *
         * @param ServiceBase $api The API class of the request, used in cases where the API changes how the fields are pulled from the args array.
         * @param array $args The arguments array passed in from the API
         * @return array Worksheet data entries
         * @throws SugarApiExceptionNotAuthorized
         */

        public function convertLead(ServiceBase $api, array $args)
        {
            $customLeadConvert = new CustomLeadConvert($args['leadId']);
            $modules = $this->loadModules($api, $customLeadConvert->getAvailableModules(), $args['modules']);

            $transferActivitiesModules =
                empty($args['transfer_activities_modules']) ? array() : $args['transfer_activities_modules'];
            $transferActivitiesAction =
                empty($args['transfer_activities_action']) ? '' : $args['transfer_activities_action'];

            $modules = $customLeadConvert->convertLead($modules, $transferActivitiesAction, $transferActivitiesModules);

            return array (
                'modules' => $this->formatBeans($api, $args, $modules)
            );
        }
    }

    Regards.

    Hats

  • Thanks again for your response hats

    Unfortunately, I seem to still be struggling with making this work. My packages' code is below. Not exactly sure what I could be doing wrong...I have tried every possible thing I can think of, and have searched everywhere it's referenced in the core code. I've run QRR, i've logged out...Help! Thanks in advance.

    Package File Name:

    Custom_Lead_Conversion_v1.zip

    Package Contents:

    Files Directory with the following files/file paths:

    ./custom/modules/Leads/CustomLeadConvert.php

    ./custom/modules/Leads/clients/base/api/CustomLeadConvertApi.php

    Manifest

    The File Contents:

    CustomLeadConvert.php

    <?php

    require_once 'modules/Leads/LeadConvert.php';

    class CustomLeadConvert extends LeadConvert
    {
        /**
         * Sets the assigned team and user based on the leads module
         * @return null
         */

        protected function setAssignedForModulesToLeads($moduleDef)
        {
            $moduleName = $moduleDef['module'];
            if (!empty($this->lead)) {
                if (empty($this->modules[$moduleName]->team_name)) {
                    $this->modules[$moduleName]->team_id = $this->lead->team_id;
                    $this->modules[$moduleName]->team_set_id = $this->lead->team_set_id;
                }
                if (empty($this->modules[$moduleName]->assigned_user_id)) {
                    $this->modules[$moduleName]->assigned_user_id = $GLOBALS['current_user']->id;
                }
            }
        }
    }

    CustomLeadConvertApi.php

    <?php

    require_once 'modules/Leads/clients/base/api/LeadConvertApi.php';

    class CustomLeadConvertApi extends LeadConvertApi
    {
        public function registerApiRest()
        {
            //Extend with extraScore property
            $api= array (
                'convertLead' => array(
                    'reqType' => 'POST',
                    'path' => array('Leads', '?', 'convert'),
                    'pathVars' => array('','leadId',''),
                    'method' => 'convertLead',
                    'extraScore' => 1, //this will help calling my custom endpoint
                    'shortHelp' => 'Custom convert Lead endpoint',
                    'longHelp' => 'modules/Leads/clients/base/api/help/LeadConvertApi.html',
                ),
            );
            return $api;
        }

        /**
         * This method handles the /Lead/:id/convert REST endpoint
         *
         * @param ServiceBase $api The API class of the request, used in cases where the API changes how the fields are pulled from the args array.
         * @param array $args The arguments array passed in from the API
         * @return array Worksheet data entries
         * @throws SugarApiExceptionNotAuthorized
         */

        public function convertLead(ServiceBase $api, array $args)
        {
            $customLeadConvert = new CustomLeadConvert($args['leadId']);
            $modules = $this->loadModules($api, $customLeadConvert->getAvailableModules(), $args['modules']);

            $transferActivitiesModules =
                empty($args['transfer_activities_modules']) ? array() : $args['transfer_activities_modules'];
            $transferActivitiesAction =
                empty($args['transfer_activities_action']) ? '' : $args['transfer_activities_action'];

            $modules = $customLeadConvert->convertLead($modules, $transferActivitiesAction, $transferActivitiesModules);

            return array (
                'modules' => $this->formatBeans($api, $args, $modules)
            );
        }
    }

    Manifest:

    <?php

    $manifest = array(
        'name' => 'Lead Module and Conversion Customization',
        'description' => 'Customizations of core Lead module',
        'author' => 'LSMG',
        'version' => '1.0',
        'is_uninstallable' => true,
        'type' => 'module',
        'acceptable_sugar_flavors' => array('PRO','ENT','ULT'),
        'acceptable_sugar_versions' => array(
            'exact_matches' => array(),
              'regex_matches' => array('(.*?)\\.(.*?)\\.(.*?)$'),
        ),
      );

    $installdefs['copy'] = array(
        0 => array(
            'from' => '<basepath>/Files/custom/modules/Leads/CustomLeadConvert.php',
            'to' => 'custom/modules/Leads/CustomLeadConvert.php',
        ),
        1 => array(
            'from' => '<basepath>/Files/custom/modules/Leads/clients/base/api/CustomLeadConvertApi.php',
            'to' => 'custom/modules/Leads/clients/base/api/CustomLeadConvertApi.php',
        ),
    );
  • Another and short way is after save logichooks but it will update and show new assigned user after save only