Field validation on transition when separate custom field value equals X

Gavin Minnis July 9, 2018

During a transition in the middle of my workflow, I use a screen to capture additional field values. However, two fields are only required when another custom field value equals "Non-PO Invoice". I'm trying to use ScriptRunner to validate these two fields.

I attempted the script as follows:

 

if (cfValues['Invoice type']?.value == 'Non-PO Invoice')


{
cfValues['Account Code'] != null && cfValues['Cost Center'] != null
}

 

This script is doing one thing correctly: it requires me to have a value in the 'Account Code' and 'Cost Center' custom fields before I transition. But this requirement is not contingent on or controlled by the value of the 'Invoice type' custom field. 

I only want those two fields to be required when "Non-PO Invoice" is selected in the 'Invoice type' custom field.

What am I doing wrong?

1 answer

1 accepted

0 votes
Answer accepted
Ivan Tovbin
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.
July 9, 2018

Hi Gavin,

I might be able to help, but first, please advise the types of your 'Invoice type', 'Account Code' and 'Cost Center' fields. I assume they are single select lists?

Cheers.

Gavin Minnis July 9, 2018

Hi Ivan,

All of the fields are single select. However, the Account Code field is an "Insight" field. Insight is an add-on by Riada. But from a script perspective, I think it too can be thought of as a standard single select field.

Gavin

Ivan Tovbin
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.
July 10, 2018

Ok. Assuming that 11111, 22222 and 33333 are IDs of your 'Invoice type', 'Account Code' and 'Cost Center' fields respectively, the code for a scripted validator is as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
CustomField invoiceType = cfMgr.getCustomFieldObject((Long) 11111)
CustomField accountCode = cfMgr.getCustomFieldObject((Long) 22222)
CustomField costCenter = cfMgr.getCustomFieldObject((Long) 33333)

if (issue.getCustomFieldValue(invoiceType).getValue().equals("Non-PO Invoice")){
if (!issue.getCustomFieldValue(accountCode) || !issue.getCustomFieldValue(costCenter)){
throw new InvalidInputException("${accountCode.getFieldName()} and ${costCenter.getFieldName()} are required!")
}
}
return true
Like kahn chang likes this
Gavin Minnis July 10, 2018

Ivan,

Thanks for this suggestion. However, I'm getting an error:

error.JPG

Ivan Tovbin
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.
July 10, 2018

You can pretty much ignore this error, it should still work. Static type checking in Scriptrunner is not always accurate.

Gavin Minnis July 10, 2018

Ivan, it worked as you said. Thanks for your help.

kahn chang May 28, 2019

Hi @Ivan Tovbin ,

I want Require “Company” if “Service” is “123”,All of the fields are single select of "Insight" field. But also Require “Company” if “Service” is not “123”.What did I miss?

Please help! Thank you!

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager cfMgr = ComponentAccessor.getCustomFieldManager()
CustomField Service = cfMgr.getCustomFieldObject((Long) 10224)
CustomField Company = cfMgr.getCustomFieldObject((Long) 11203)

if (issue.getCustomFieldValue(Service).getValue().equals(“123”)){
if (!issue.getCustomFieldValue(Company)){
throw new InvalidInputException("TEST")
}
}
return true

Suggest an answer

Log in or Sign up to answer