Let the platform do the work

SetValue

Overview

The SugarLogic SetValue action, located in ./include/Expressions/Actions/SetValueAction.php, is used to set the value of a field based on a formula.

Implementation

While the dependency metadata for your module can be defined in ./modules/<module>/metadata/dependencydefs.php and  ./custom/modules/<module>/metadata/dependencydef.php, it is recommended to use the extension framework when customizing stock modules to prevent third party plugins from conflicting with your customizations. The following section will demonstrate how to implement a read-only dependency.

SetValue Parameters

Parameter Type Description
target String The name of the field to target for visibility.
value String SugarLogic formula used to get the value for the target field.

For more information on the various parameters in the dependency definitions, please refer to the dependency actions documentation.

Examples

The follow sections outline the various ways this dependency can be implemented.

Dependency Extensions

For our example, we will create a dependency on the Leads module that will display the number of activities related to a Lead.  Activities are composed of calls, meetings, tasks, notes, and emails. An example extension definition is shown below:

./custom/Extension/modules/<module>/Ext/Dependencies/custom_phone_alternate.php

  <?php

$dependencies['Leads']['activity_count_dep'] = array(
    'hooks' => array("edit", "view"), //not including save so that the value isn't stored in the DB
    'trigger' => 'true', //Optional, the trigger for the dependency. Defaults to 'true'.
    'onload' => true, //Whether or not to trigger the dependencies when the page is loaded
    'actions' => array(
        array(
            'name' => 'SetValue',
            'params' => array(
                'target' => 'activity_count_c',
                'value' => 'add(
                    count($notes),
                    count($calls),
                    count($emails),
                    count($meetings),
                    count($tasks)
                )'
            )
        )
    )
);

Once you have the file in place, you will need to navigate to Admin > Repairs > and run a Quick Repair and Rebuild.

Note: It is important that the module name is plural ('Cases' vs. 'Case') and that the name of the dependency, "activity_count_dep" in this example, is unique.

Chaining Dependencies

You can also add as many actions as you need to the array. In the example below, we want to display our count value but prevent users from being able to edit the value. An example extension definition is shown below:

  <?php

$dependencies['Leads']['number_of_cases_dep'] = array(
    'hooks' => array("edit", "view"), //not including save so that the value isn't stored in the DB
    'trigger' => 'true', //Optional, the trigger for the dependency. Defaults to 'true'.
    //'triggerFields' => array('status'), //unneeded for this example as its not field triggered
    'onload' => true,
    'actions' => array(
        array(
            'name' => 'SetValue',
            'params' => array(
                'target' => 'activity_count_c',
                'value' => 'add(
                    count($notes),
                    count($calls),
                    count($emails),
                    count($meetings),
                    count($tasks)
                )'
            )
        ),
        array(
            'name' => 'ReadOnly',
            'params' => array(
                'target' => 'activity_count_c',
                'value' => 'true',  //Set to true instead of a formula because its always read-only
            ),
        )
    )
);

Once you have the file in place, you will need to navigate to Admin > Repairs > and run a Quick Repair and Rebuild.

Note: It is important that the module name is plural ('Cases' vs. 'Case') and that the name of the dependency, "number_of_cases_dep" in this example, is unique.

Dependencies in Field Definitions

Unlike several of the other dependencies, SetValue is built into Studio.  So this dependency can be set as a custom vardef value or in the vardefs file of a custom module.  If you wanted to add this dependency to an existing field then you can create a vardef extension such as ./custom/Extension/modules/<module>/Ext/Vardefs/. An example extension definition is shown below:

./custom/Extension/modules/Accounts/Ext/Vardefs/activity_count_c.php

  <?php

$dictionary['Lead']['fields']['activity_count_c']['options'] = 'numeric_range_search_dom';
$dictionary['Lead']['fields']['activity_count_2_c']['calculated'] = 'true';
$dictionary['Lead']['fields']['activity_count_2_c']['formula'] = 'add(
    count($calls),
    count($emails),
    count($meetings),
    count($notes),
    count($tasks)
)';
$dictionary['Lead']['fields']['activity_count_2_c']['enforced'] = 'true';
$dictionary['Lead']['fields']['activity_count_2_c']['enable_range_search'] = '1';

Once you have the file in place, you will need to navigate to Admin > Repairs > and run a Quick Repair and Rebuild.