Hello!
I'm writing a scriptrunner validator on create that checks the values of certain fields and lets the ticket be created accordingly.
Script below:
issue.customfield_14690.value == "No" ? true :
issue.customfield_14690.value == "Yes" && issue.customfield_14613 ? true :
issue.customfield_14690.value == "Yes" && (issue.customfield_14440 || issue.customfield_14611) ? true :
false
When I test this in scriptrunner directly with a ticket that has cf_14690 as Yes, and with cf_14613, cf_14440, and cf_14611 as null, I get a false, however when I actually go to create the ticket, it creates just fine.
Why is the validator not actually stopping the create transition from happening?
Edit:
Also tried the following script variations with the same results:
(issue.customfield_14690.value == "No" ||
(issue.customfield_14690.value == "Yes" && issue.customfield_14613 != null) ||
(issue.customfield_14690.value == "Yes" && (issue.customfield_14440 != null || issue.customfield_14611 != null)))
if (issue?.customfield_14690?.value == "No") {
true
} else {
if (issue?.customfield_14690?.value == "Yes" && issue?.customfield_14613 != null){
true
}
else {
if(issue?.customfield_14690?.value == "Yes" && (issue.customfield_14440 != null || issue.customfield_14611 != null)){
true
}
else {
false
}
}
}Thanks!
Hello Pierre,
the issue is how you're reading the field values inside a Simple Scripted Validator on a create transition. The issue binding at create time does not yet reflect what the user is typing into the create screen, so issue.customfield_14690.value is reading from an issue that essentially does not exist yet. The script either evaluates against null fields and silently throws, or hits a branch that returns truthy, and the transition goes through.
The binding that exposes the values currently entered on the transition screen is cfValues, keyed by field name (not by id). That is what the ScriptRunner docs use for this exact pattern, including an example that is structurally identical to yours: a Yes/No select that conditionally requires another field.
Rewritten against your three fields (replace the names with the actual display names of customfield_14690, customfield_14613, customfield_14440, and customfield_14611 in your instance):
cfValues['Your Yes/No Field']?.value == 'No' ||
(cfValues['Your Yes/No Field']?.value == 'Yes' &&
(cfValues['Field 14613'] ||
cfValues['Field 14440'] ||
cfValues['Field 14611']))
Two things to double-check on the transition itself:
cfValues only sees fields that are actually on the transition screen.cfValues must match the custom field's display name exactly, including case.The full reference, including the Yes/No conditional-required-field example I based the rewrite on:
https://docs.adaptavist.com/sr4js/latest/features/workflows/validators/simple-scripted-validators
One side note on why your test in the ScriptRunner console returned false correctly but the create still succeeded: the console runs against a persisted issue you pass in, where issue.customfield_XXXXX works fine because the values are already on the database row. During a create transition there is no persisted row yet, which is exactly the gap cfValues is designed to bridge.
I am looking to your feedback.
Greetings,
Alex
Hi Alex,
Your explanation makes perfect sense, I'll keep it in mind for the next validator I need to write, or in case the one I did end up writing starts acting wonky.
After a lot of trial and error I ended with the following script that seemed to work in all cases:
if (issue?.customfield_14690?.value == "No") { // Contractual Commitment
true // pass if customfield_14690 is No
} else {
if (issue?.customfield_14690?.value == "Yes" && issue?.customfield_14613 != null){
true // pass if customfield_14690 is yes and customfield_14613 is populated
}
else {
if(issue?.customfield_14690?.value == "Yes" && ((issue.customfield_14440 != null && issue.customfield_14440 != []) || issue.customfield_14611 != null)){
true // pass if customfield_14690 is yes and (issue.customfield_14440 is populated or issue.customfield_14611 is populated)
// we're checking both if issue.customfield_14440 is null (no value) or if it's an empty array. The issue picker plugin populates as arrays
}
else {
false
}
}
}
Thank you again for your detailed response, greatly appreciated!
Pierre
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Since this is a Validator for the Jira Cloud instance, I suggest either using ScriptRunner's Expression Builder or the ScriptRunner Migration Suite (SMS) to help with the Expressions.
Both instances are using AI to assist. I've used both of these, and the expression generated for the Validators and Conditions works perfectly.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.