Make a custom field required based on another custom field

Make a custom field required based on another custom field


Required apps

Power Scripts™ for Jira (server)

Level: INTERMEDIATE

Problem

We have a custom field Delivery (id customfield_10100) of type select list with options Yes or No and another custom field called Address (id customfield_11100).

If the user selects Yes in the Delivery field, we need to mark the Address field as required; if the user selects No, then the Address field should not be required.

Solution

This behavior can be achieved on any transition screen by using a SIL™ Validator on that particular transition, as described here: Force a custom field to be required if another field was set to a certain value.

However, to achieve this behavior also on the edit issue screen, we have to make use of the Live Fields feature of Power Scripts™ for Jira:

Configure the Initial Script for Live Fields

First you have to create the following script and associate it with the main Live Fields configuration on your project, as described here.

initial_script.sil
lfWatch("customfield_10100", {"customfield_11100", "customfield_10100"}, "hook.sil");
lfWatch("customfield_11100", {"customfield_11100", "customfield_10100"}, "hook.sil", {"keyup"});

The hook.sil script will now be executed every time the value for Delivery field changes and whenever the Address field receives an input.

For more information about hooks see How 'Live Fields' work, as well as the lfWatch routine documentation.

Create the Hook Script for Live Fields

Now we will create the hook.sil script, responsible for marking the Address field required or not.

When the Delivery field is set to Yes and the Address field is empty, a warning message is displayed to the Address field informing that the field is required and the submit button is disabled.

When Delivery is set to No or when Address receives an input the message disappears and the submit button is re-enabled.

hook.sil
if(argv["customfield_10100"] == "Yes" && argv["customfield_11100"] == "") {
   lfShowFieldMessage("customfield_11100", "Field Address is required.", "WARNING");
   lfDisable("editSubmit");
} else {
   lfHideFieldMessage("customfield_11100");
   lfEnable("editSubmit");
}

If you want the same behavior for transition screens without using SIL validators, you can add in the hook script a call to lfDisable("transitionSubmit") and lfEnable("transitionSubmit"), just as for the editSubmit button.

This will disable the transition submit button when Address field becomes required.

Testing it

When opening the edit issue screen and choosing Yes in the Delivery select list, field Address is marked as required and the Update button is disabled, until Address receives an input:

That's it. You can make any Jira field required in any screen based on other fields values using Power Scripts™ for Jira's Live Fields capabilities.

See also