Let the platform do the work

Customizing Core SugarBeans

Overview

This article covers how to extend core SugarBean objects to implement custom code. This can be used to alter the stock assignment notification message or to add logic to change the default modules behavior.

Customizing a Core Module

The best approach to customizing a core SugarBean object is to become familiar with how the core Bean operates and only modify the bean logic where absolutely necessary. After understanding where you wish to make your customization in the module Bean class, you can extend the class in the custom directory. In order to avoid duplicating code from the core Bean class, it is always best to extend the class rather than duplicate the class to the custom directory. 

Extending the SugarBean

For this example, we will look at modifying the Leads SugarBean object. To extend the core Leads bean, create the following file:

./custom/modules/Leads/CustomLead.php

  <?php

require_once 'modules/Leads/Lead.php';

class CustomLead extends Lead
{
    /**
     * Saves the record
     * - You can use this method to implement before save or after save logic
     *
     * @param bool $check_notify
     * @return string
     */
    function save($check_notify = FALSE)
    {
        $id = parent::save($check_notify);
        return $id;
    }

    /**
     * Retrieves a record
     * - You can use this method to set properties when fetching a bean
     *
     * @param string $id
     * @param bool $encode
     * @param bool $deleted
     * @return $this
     */
    function retrieve($id = '-1', $encode = true, $deleted = true)
    {
        return parent::retrieve($id, $encode, $deleted);
    }

    /**
     * Calls custom logic events
     * - You can use this method to watch for specific logic hook events
     *
     * @param $event
     * @param array $arguments
     */
    function call_custom_logic($event, $arguments = array())
    {
        parent::call_custom_logic($event, $arguments);
    }
}

Register the Custom Class

Once the custom SugarBean class file has been created, register that class to be used by the module so that the BeanFactory knows which class to use. To register the class you will create the following file in the ./custom/Extension/ directory:

./custom/Extension/application/Ext/Include/customLead.php

  <?php

/**
* The $objectList array, maps the module name to the Vardef property
* By default only a few core modules have this defined, since their Class/Object names differs from their Vardef Property
**/
$objectList['Leads'] = 'Lead';

// $beanList maps the Bean/Module name to the Class name
$beanList['Leads'] = 'CustomLead';

// $beanFiles maps the Class name to the PHP Class file
$beanFiles['CustomLead'] = 'custom/modules/Leads/CustomLead.php';

Note: The $objectList array only needs to be set on those modules that do not have it set already. You can view ./include/modules.php to see the core modules that have it defined already.

Once the registration file is in place, go to Admin > Repairs, and run a Quick Repair and Rebuild so that the system starts using the custom class.

Things to Note

Overriding a core SugarBean class comes with some caveats:

  1. The custom class is only used when the product core code uses the BeanFactory
    • For most circumstances, Sugar is set up to use BeanFactory, however, legacy code or specific logic that is pairs core modules together may use direct calls to a core SugarBean class, which would then cause the custom class to not be used. In those scenarios, it is recommended to use a logic look instead.

  2. Extending the Cases module doesn't affect the email Import process.
  3. If the $objectList property is not defined for the module, the custom object will be used, however, things like Studio will no longer work correctly.

    • For the example above, we defined the $objectList property for the Leads module to make sure that Studio continued working as expected. Without this definition, if you navigate to Admin > Studio, fields and relationships will not render properly.