Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Require Component/s field based on a value selected from a custom field

John Diaz February 14, 2022

I need helping creating a validator script (I think) to make the Component/s field required based what the user selects from a single select custom field.

Use Case:

1. Single select drop-down field called "Atlassian Platform" with two value options: Confluence, Jira. 

2. If user selects "Jira" from the "Atlassian Platform" field then the Component/s field is required (nothing happens if user selects "Confluence")

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
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 14, 2022

It is possible to achieve this with either a Behaviour or a validator.

Behaviour scripts are capable of knowing which part of the workflow you are in. This is a good choice if you want to prevent users from clearing the component using the edit screen after it's deemed required.

Of course, a validator will work inherently within the workflow but will allow users to edit the component after the transition has taken place.

A Simple Scripted validator would look like this:

cf["Atlassian Platform"].value != 'Jira' || issue.components

Simple Scripted validators must return a true value to pass or false to generate an error.

 

A behaviour script would be a little more complicated:

if(!underlyingIssue) return //don't run the validator on create screen when the underlyingissue does not yet exist

def transitionToMakeCompoenntRequired = 'name of transition'
def statusesAfterThatTransition = ['status1', 'status2']
def componentsField = getFieldById('components')
def platformField = getFieldByName('Atlassian Platform')

def isTargetAction = actionName == transitionToMakeCompoenntRequired 
def isTargetStatus = underlyingIssue.status.name in statusesAfterThatTransition
def isRequiredValue = platformField.value == 'Jira'

def componentRequired = (isTargetAction || isTargetStatus) && isRequiredValue
componentField.setRequired(componentRequired)
John Diaz February 14, 2022

@Peter-Dave Sheehan Unfortunately the simple scripted validator didn't work.  I also ended up having to change my custom field name from Atlassian Platform to AM Application. 

Screencapture.PNG

Peter-Dave Sheehan
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 14, 2022

Sorry... I was sloppy. It should be "cfValues", the built-in variable that scriptrunner offers that includes all the custom field values in an easily accessible map.

cfValues["AM Application"].value != "JIRA" || issue.coponents
John Diaz February 15, 2022

Bummer, still not working. I tried with both single quotes and double quotes.

cfValues['AM Application'].value != 'JIRA' || issue.coponents 
Peter-Dave Sheehan
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 15, 2022

I was clearly under-caffeinated yesterday ... there was another typo: coponents vs components

cfValues["AM Application"].value != "JIRA" || issue.components
John Diaz February 15, 2022

Oops, I meant to say that caught the misspell but it's not working. 

Peter-Dave Sheehan
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 15, 2022

Can you share some more details? Are there any errors in the script editor?

Do you see errors in the execution log (open the workflow/transition after executing the transition to view execution logs).

Maybe we can add some additional logging to help with identifying where it's failing:

log.info "'AM Application' is ${cfValues["AM Application"].value}"
log.info "issue.compoenents is $issue.components"
log.info "am application check is:${cfValues["AM Application"].value != "JIRA"}"
log.info "issue.component check is ${issue.components ? true : false}"
log.info "final validation result is ${cfValues["AM Application"].value != "JIRA" || issue.components}"

cfValues["AM Application"].value != "JIRA" || issue.components
John Diaz February 18, 2022

Here's a screenshot of the results and logs.  Also, shouldn't it be == "JIRA" instead != "JIRA" because I only want Component/s field to be required if JIRA is selected. 


 

Script Console_1.PNGScript Console_2.PNG

Peter-Dave Sheehan
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 18, 2022

The script will not work in the Script Console.

In a simple scripted validator script editor window, there will be some built-in variables and objects (you can see which by clicking the question mark below the editor).

issue and cfValues is two of those variables.

These variable are not available in the console since it's not aware of what ticket you want to act on etc.

As for the logic for the selected value... you want to return a "true" value (i.e. allow the transition to continue) either when the AM Application is something OTHER than Jira, OR if they've selected JIRA, then the components must be specified.

The way an OR (||) is processed, is that it will return true at the first instance that is true.

So from left to right, the first statement evaluated will be "AM Application" different than JIRA. If true, then return true and allow the transition. If False (AM Application IS JIRA) then evaluate the next statement. 
Groovy has "thrutiness" shortcuts. So here, we are essentially validating that issue.components.size() > 0.

So, the final rule is, when AM Application is JIRA at least one component must be present.

0 votes
Ravi Sagar _Sparxsys_
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 14, 2022

Hi @John Diaz 

Using ScriptRunner Behaviours will let you do that easily. Take a look at this script.

Ravi

John Diaz February 14, 2022

@Ravi Sagar _Sparxsys_  I appreciate the quick response.  Unfortunately, Behavior isn't going to work for me.  

There's a certain part of the workflow where I want someone on my team to be forced to enter a Component when moving the ticket "In Progress" ONLY IF "Jira" is selected from the "Atlassian Platform" field.  So Component isn't required at the start of the ticket creation and will not be required if "Confluence" is selected. 

Apologies for not providing enough context with what I'm trying to accomplish. 

TAGS
AUG Leaders

Atlassian Community Events