This article will walk through how to use the related() and equal() functions in a Sugar Logic formula for a dependent field that should only appear based on the value of a specified field in a related module.
Dependent Fields use Sugar Logic to control the fields’ visibility so that they are only shown when certain conditions are met. Administrators can create these formulas via Studio or Module Builder when editing a field by selecting the Dependent checkbox and using formula builder. For an overview on Sugar Logic and Calculated Fields, please refer to the article Introduction to Calculated Fields.
In this example, we will make the custom Status field on a Contact record appear only if the related Account record’s Account Type field is set to “Customer”.
There must be a defined relationship between the two modules in order for the dependency formula to work. You will need to create a new field of type TextField in the Contacts module and mark it as a dependent field before entering the following formula.
The dependent formula below will display the Status field in the edit and detailview of the contact record only if the value of the Account Type field in the related Account record equals the specified value of “Customer”.
When the Account Type field is set to “Customer”, the formula finds a match with the specified value “Customer”. The Status field is visible on the Contact record since the condition is met.
Since we need to access the account_type field from the accounts module, w e will use the related() function to reach across to the related module and access the desired field. The related() function needs both the module name (or relationship name in the case of a custom relationship) as well as the desired field’s name. You can use Studio’s Formula Builder’s Related Field button to produce the right module or relationship name.
Since account_type is a dropdown field, related() will return the selected option’s item name (as opposed to its display label) as the value. You can see the item names and display labels for all options within a dropdown menu in Admin > Dropdown Editor.
After retrieving our related account’s type, we use the equal() function to compare the result with our desired dropdown item name, “Customer”. The function returns “true” if the two strings match or “false” if they do not.
For our example, the related account’s type is indeed set to “Customer”, which causes the related() function to return the string “Customer”. The equal() function then finds a match to our provided string, and evaluates to “true” which causes our dependent Status field on the Contact record to be visible.
Once your custom text field is created with the dependency formula and added to the module’s edit and detailview, the dependent field will appear if the related module displays the specified value. For our example, the Status field is visible since the select option in the related account’s Type field has an item name of “Customer”.