Need help with scriptrunner script validator to make field mandatory

Faraz_Khan February 25, 2020

Hi,

I have a project with multiple custom issue types

I have 2 custom fields

1. Priority ( select list ) (customfield_10043)

2. Reason for urgency (text field) (customfield_10044)

The above 2 fields are non mandatory and also not present for all issue types

I want to write a scriptrunner script validator on create transition which checks that if Priority ='Critical and urgent' then reason for urgency field should be mandatory.

I wrote something like this 

issue.customfield_10043.value=="Critical and Urgent" && issue.customfield_10044.value != ""

the above script doesnt seem to work properly.

When i try to create an issue where these fields are not available, the validator still fires and doesnt allow issue creation

for issues that do have both these fields, validator throws an error if the leave Priority as None (None being auto injected by JIRA as this is not a required field)

I can find how to add a null checking condition in the validator . Also validator tells me that 'Script Validators ONLY use Jira Expressions'

Any help is deeply appreciated.

Thanks ! 

2 answers

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 25, 2020

Hi

I can confirm the answer given by Suhas will not work with ScriptRunner for Jira Cloud as behaviors are only exist inside of ScriptRunner for Jira Server and do  not exist inside of ScriptRunner for Jira Cloud as described here

I can confirm that the ScriptRunner for Jira Cloud plugin provides workflow Validators using the Jira Expression Framework as documented in the Atlassian API documentation page located here or in the documentation page located here which explains how to add in a new validator to your workflow.

When viewing the Jira Expression Framework API documentation page linked above you can navigate to the Context variables section which shows what variables are provided by this framework that can be used to create the expression.

If you then click on one of the variables it will show all the properties that can be called on the variable for the expression such as for the issue variable as shown here. 

You will notice when reviewing the Jira Expression Framework documentation that it shows you must reference the Priority field by its name of Priority and that it only supports checking values using the name and Id objects as described here.

Also you can see in the Jira Expression Framework documentation page on the Issue Variable how to check custom field values as well as a list of all comparison operators which are supported in the page here.

You will be able to use the Jira Expression Framework documentation pages linked above along with the documentation on the validators feature as reference guides, to see how to construct the validator that you require.

Finally using validators, I can confirm that you can mandate that both the Priority and Jira Expression Framework fields are required but you cannot make the Reason for Urgency field only mandatory when certain fields are selected as the Jira Expression Framework provided by Atlassian does not support this.

I hope this information helps.

Regards,

Kristian

0 votes
Suhas P
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2020

You should use Script Runner Behaviour (its part of Script runner) instead of a workflow validator.

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html

 

Try following code

def prio = getFieldById("customfield_XXXX") 

def RFU = getFieldById("customfield_XXXX") //Reason for urgency

if (getActionName()=="Create") {

if(prio.value.toString() =~"Critical & Urgent"){

RFU.setRequired(true)

}

}

Suhas P
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2020

def prio = getFieldById("customfield_XXXX") 

def RFU = getFieldById("customfield_XXXX") //Reason for urgency

if (getActionName()=="Create") {

if(prio.value.toString() =~"Critical & Urgent"){

RFU.setRequired(true)

}

}

Faraz_Khan February 25, 2020

Thanks for the reply!

Apologies for the novice question, but from where can I create a behavior? I cannot find any such option even after following link that you posted .

I am on JIRA cloud and using scriptrunner trial

Faraz_Khan February 25, 2020

i am afraid, JIRA cloud does not support behaviors

Validators are also very limited to just JIRA expressions I guess

Suhas P
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 25, 2020

Sorry my bad, I didn't see Jira Cloud tag. Yes, Jira cloud doesn't have behaviour. 

Suggest an answer

Log in or Sign up to answer