Sugar: Setting Default For Cases and Opportunities

  • Updated

Riva Cloud and Riva On-Premise do not force specific unknown or customizable values on new items; the built-in Sugar Custom Hooks are recommended.

Custom Hooks have been available in Sugar for many versions and are fully supported. They provide the greatest flexibility, allowing different defaults or custom processing based on different key values. A custom hook can be built to copy the values from a "Template" object that could be edited in the CRM, allowing users to override their own individual defaults. An unlimited number of options are available.

This article includes some examples that would work with Sugar 4.5+ to Sugar 6.x. These examples have not been confirmed against Sugar 7.x or SuiteCRM.

Example for Cases

Filename: sugarcrm_root/custom/include/Cases/CaseHooks.OnCreate_SetDefaults.php

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('data/SugarBean.php');
require_once('modules/Users/User.php');
require_once('include/utils.php');

class CaseHooks_OnCreate_SetDefaults
{        
    function Execute(&$bean, $event, $arguments)
    {
        /// In-case we are miss-hooked
        if ($event != 'before_save')
            return;
            
        /// Should only perform on new items.
        if(!empty($bean->fetched_row))
            return;
        
        /// Set Priority
        if(!isset($bean->priority))
        {
            // $bean->priority = "P1"; /// High
            $bean->priority = "P2"; /// Normal
            // $bean->priority = "P3"; /// Low
            // $bean->priority = "P0"; /// Urgent
        }
        
        /// Set Status to "Active - New"
        if(!isset($bean->status))
        {
            $bean->status = "Active - New";
        }
    }
}
?>

Filename: sugarcrm_root/custom/modules/Cases/logic_hooks.php

Note: If the file already exists, just copy the $hook_array  line.

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

/// Hook Array Definition:    Array(SORT POSITION, DESC_NAME, FILE_PATH_NAME, CLASS_NAME, FUNCTION_NAME);

$hook_array['before_save'][] = Array(99, 'Defaults', 'custom/include/Cases/CaseHooks.OnCreate_SetDefaults.php','CaseHooks_OnCreate_SetDefaults', 'Execute');

?>

Example for Opportunities

Filename: sugarcrm_root/custom/include/Opportunities/OpportunityHooks.OnCreate_SetDefaults.php

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('data/SugarBean.php');
require_once('modules/Users/User.php');
require_once('include/utils.php');

class OpportunityHooks_OnCreate_SetDefaults
{
    function Execute(&$bean, $event, $arguments)
    {
        /// In-case we are miss-hooked
        if ($event != 'before_save')
            return;
           
        if(!empty($bean->fetched_row))
            return;
       
        if(!isset($bean->forecast_category))
        {
            $bean->forecast_category = "Pipeline";
        }
       
        if(!isset($bean->sales_stage))
        {
            $bean->sales_stage = "Prospecting";
        }
       
        if(!isset($bean->probability))
        {
            $bean->probability = "10";
        }
       
        if(!isset($bean->date_closed))
        {
            $bean->date_closed = date("Y-m-d", strtotime("+30 day"));
        }
       
        if(!isset($bean->amount))
        {
            $bean->amount = 0;
        }
    }
}

?>

Filename: sugarcrm_root/custom/modules/Opportunities/logic_hooks.php

Note: If the file already exists, just copy the $hook_array line.

<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

/// Hook Array Definition:    Array(SORT POSITION, DESC_NAME, FILE_PATH_NAME, CLASS_NAME, FUNCTION_NAME);

$hook_array['before_save'][] = Array(99, 'OnCreate_SetDefaults', 'custom/include/Opportunities/OpportunityHooks.OnCreate_SetDefaults.php', 'OpportunityHooks_OnCreate_SetDefaults', 'Execute');

?>

Was this article helpful?

/