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")
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)
@PD 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bummer, still not working. I tried with both single quotes and double quotes.
cfValues['AM Application'].value != 'JIRA' || issue.coponents
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was clearly under-caffeinated yesterday ... there was another typo: coponents vs components
cfValues["AM Application"].value != "JIRA" || issue.components
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @John Diaz
Using ScriptRunner Behaviours will let you do that easily. Take a look at this script.
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.