Let the platform do the work

SetOptions

Overview

The SugarLogic SetOptions action, located in ./include/Expressions/Actions/SetOptionsAction.php, is used to set the options list of a dropdown 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.

Setoptions Parameters

Parameter Type Description
target String The name of the dropdown field that you want to change the option list for
keys String A formula used to get the option list keys for the target field or a list name from which keys will be extracted.
labels String A formula used to get the option list labels for the target field or a list name from which labels will be extracted.

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

Examples

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

Using an Existing DropDown List

You can also set the options list to any current options list already in the system  For example if you wanted to have the industry dropdown in Accounts show the 'bug_type_dom' list from Bugs you could do this

  <?php

$dependencies['Leads']['setoptions_industry'] = array(
   'hooks' => array("edit","save"),
   'trigger' => 'true',
   'triggerFields' => array('industry'),
   'onload' => true,
   'actions' => array(
     array(
       'name' => 'SetOptions',
       'params' => array(
         'target' => 'industry',
         'keys' => 'getDropdownKeySet("bug_type_dom")',
         'labels' => 'getDropdownValueSet("bug_type_dom")'
       ),
     ),
   ),
);

This would grab the keys and label from the 'bug_type_dom' using the getDropdownKeySet() and getDropdownValueSet() JavaScript functions and display them instead of the normal list. 

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 ('Accounts' vs. 'Account') and that the name of the dependency, "setoptions_industry" in this example, is unique.

Complex Dynamic Lists

For our first example, we will change a dropdown called fruits_c to include only fruits that are in season.  This could be done with a dependent dropdown but that would require the user to pick the proper season.  With this, we can have a function that returns only fruit that is in season right now.  I added the dropdown fruits_c to Leads and created a new list for it that looks like this

  $app_list_strings['fruits_list']=array (
	'Apples' => 'Apples',
	'Strawberries' => 'Strawberries',
	'Mangos' => 'Mangos',
	'Pineapples' => 'Pineapples',
	'Blackberries' => 'BlackBerries',
	'BlueBerries' => 'BlueBerries',
);

To keep it simple I made the labels and the keys the same.

Then I extended SugarLogic as outlined in Extending SugarLogic and made a new function called fruitInSeason() that returns a string reflecting what fruit is in season right now.  To work for the createList() function it would return a list like "Apples","Mangos","BlueBerries".

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

  <?php

$dependencies['Leads']['setoptions_fruit'] = array(
   'hooks' => array("edit","save"),
   'trigger' => 'true',
   'triggerFields' => array('fruits_c'),
   'onload' => true,
   'actions' => array(
      array(
        'name' => 'SetOptions',
        'params' => array(
           'target' => 'fruits_c',
           'keys' => "createList(fruitInSeason())",
           'labels' => "createList(fruitInSeason())"
        ),
      ),
    ),
);

The createList() function is a JavaScript function from Sidecar.  It requires a comma delimited quote enclosed list of options. 

We only want this to affect EditViews and Saves, not normal record views since they need to be able to display all fruits and not a truncated list of them.  So we make the 'hooks' array

  'hooks' => array("edit","save"),

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 ('Leads' vs. 'Lead') and that the name of the dependency, "setoptions_fruit" in this example, is unique.