Overview
In this discussion, I will show you how to use a simple custom coding method to make a calculated field optionally editable by a user. I will demonstrate this with a custom probability field. That is, with an integer field in Opportunities that holds a different numerical value for each of the options in Sales Stage.
This field will use a Sugar Logic formula, but it will be installed as a custom dependency instead of being input in Studio so that we can add a checkbox field, which we can check whenever we want to manually set the probability.
Prerequisites
- Using Studio, create a custom integer field in Opportunities named sales_probability_c.
- Using Dropdown Editor, ensure sales_stage_dom and sales_probability_dom dropdown lists have matching Item Names
- Using Studio, create a custom checkbox field in Opportunities named manual_probability_c
- Using Studio, add the fields to the Opportunities RecordView layout.
Add A Custom Dependency
Note: If you are hosted by SugarCRM or by another host that restricts you from direct file system access, you can package the following file into a Module Loader package and install it using Module Loader.
- Create a custom file at:
custom/Extension/modules/Opportunities/Ext/Dependencies/manual_probability.php
- Populate the file with the following code:Fullscreen123456789101112131415$dependencies['Opportunities']['sales_probability_c'] = array('hooks' => array("edit","save"), //options: view, edit, save, all'trigger' => 'or(equal($manual_probability_c,0),equal($manual_probability_c,false))','triggerFields' => array(),'onload' => true,'actions' => array(array('name' => 'SetValue','params' => array('target' => 'sales_probability_c','value' => 'getDropdownValue("sales_probability_dom",$sales_stage)'),),),);
The Result
With the two fields on the RecordView layout, the probability field will automatically calculate during edit and save. By defaut, the checkbox will be unchecked. When you don't want the calculation to run for a specific record, check the checkbox. With the checkbox checked, users can manually set the probability field as desired. As soon as you want the system to automatically set the value again, simply uncheck the checkbox and save!
More About Custom Dependency Settings
The following information can be found here in SugarCRM's documentation.
hooks : Defines when the dependency should be evaluated. Possible values are edit (on edit/quickCreate views), view (Detail/Wireless views), save (during a save action), and all (any time the record is accessed/saved).
trigger : A boolean formula that defines if the actions should be fired. (optional, defaults to 'true')
triggerFields : An array of field names that when when modified should trigger re-evaluation of this dependency on edit views. (optional, defaults to the set of fields found in the trigger formula)
onload : If true, the dependency will be fired when an edit view loads (optional, defaults to true)
actions : An array of action definitions to fire when the trigger formula is true.
notActions : An array of action definitions to fire when the trigger formula is false. (optional)
Additional Credits
Below are credits to additional resources that have provided knowledge I leveraged to write this discussion post. Thank you!