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?
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.
You can pretty much ignore this error, it should still work. Static type checking in Scriptrunner is not always accurate.
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.
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
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.