Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Scriptrunner validator in a transition, how can I add the value of fields if a field is null?

Cash Coyne
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.
January 22, 2024

I have a transition in a workflow and want to make sure that at least one of a group of number fields is populated.  I tried this:

issue.customfield_11667 != null || issue.customfield_11668 != null || issue.customfield_11669 != null || issue.customfield_11670 != null || issue.customfield_11671 != null || issue.customfield_11672 != null || issue.customfield_11673 != null || issue.customfield_11674 != null || issue.customfield_11679 != null || issue.customfield_11680 != null || issue.customfield_11681 != null || issue.customfield_11682 != null || issue.customfield_11683 != null || issue.customfield_11675 != null

But it said that there was a limit of 10 expressions and errored

Then I tried this:

(issue.customfield_11667|0 + issue.customfield_11668|0 + issue.customfield_11669|0 + issue.customfield_11670|0 + issue.customfield_11671|0 + issue.customfield_11672|0 + issue.customfield_11673|0 + issue.customfield_11674|0 + issue.customfield_11679|0 + issue.customfield_11680|0 + issue.customfield_11681|0 + issue.customfield_11682|0 + issue.customfield_11683|0 + issue.customfield_11675) > 0

And it said "Evaluation failed: "issue.customfield_11667 + issue.customfield_11668" - operator "+" is not applicable to types: null and null"

Then I tried this, thinking it might work like with smart values:

(issue.customfield_11667|0 + issue.customfield_11668|0 + issue.customfield_11669|0 + issue.customfield_11670|0 + issue.customfield_11671|0 + issue.customfield_11672|0 + issue.customfield_11673|0 + issue.customfield_11674|0 + issue.customfield_11679|0 + issue.customfield_11680|0 + issue.customfield_11681|0 + issue.customfield_11682|0 + issue.customfield_11683|0 + issue.customfield_11675) > 0

And that errored ... is there a better/correct syntax for that?

And obviously checking for nulls for each field would have me hitting the 10 expression limit again.

And I can't break it up into two separate validators because you can't "or" validators

How can I either check to make sure at least one of these 13 fields has a value or add the values of this many fields that may be null?

1 answer

0 votes
Aswin Raj D
Community Champion
January 22, 2024

Dear @Cash Coyne ,

 

One way to work around this limitation is to use a scripted validator with a custom scripted validator script that checks for the condition you want.

Here's an example script that you can use:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.CustomField

def fieldsToCheck = [
    'customfield_11667',
    'customfield_11668',
    'customfield_11669',
    'customfield_11670',
    'customfield_11671',
    'customfield_11672',
    'customfield_11673',
    'customfield_11674',
    'customfield_11679',
    'customfield_11680',
    'customfield_11681',
    'customfield_11682',
    'customfield_11683',
    'customfield_11675'
]

def fieldManager = ComponentAccessor.fieldManager
def atLeastOneFieldHasValue = fieldsToCheck.any {
    CustomField customField = fieldManager.getCustomField(it)
    issue.getCustomFieldValue(customField) != null
}

if (!atLeastOneFieldHasValue) {
    return false
}

return true
Cash Coyne
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.
January 23, 2024

I get this error:

Jira expression failed to parse: line 1, column 1: expression expected, reserved keyword 'import' encountered.

Is that because I'm on Jira Cloud?

Under the code window, it says "Script Validators ONLY use Jira Expressions"

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events