Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Workflow validator based on custom field values

Eduardo Marques
Contributor
June 13, 2023

I have a date and time field that is blank by default but I would like the field to be mandatory at ticket creation if a certain option is selected in a different custom field.

Scenario:

Option 1

If Option A of Field A (dropdown list single choice) is selected, Field B (date & time field) is not mandatory and the ticket can be created.

Option 2

If Option B or C of Field A (dropdown list single choice) is selected, Field B (date & time field) is mandatory and the ticket cannot be created.

I have tried to use a "Simple Scripted Validator" from Scriptrunner without success.

My last code attempt were:

(cfValues['Field A'] == 'Option B' && cfValues['Field B'] != null)

(cfValues['Field A'] == 'Option B' && cfValues['Field B'] == null)

(cfValues['Field A'] == 'Option B'  ||  cfValues['Field B'] != null)

(cfValues['Field A'] == 'Option B'  ||  cfValues['Field B'] == null)

 

All of them failed either by allowing the creation when it should be blocked or by blocking the creation when it should be allowed.

 

Can anyone help with this code?

Thank you

2 answers

2 accepted

3 votes
Answer accepted
Evgenii
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2023

Hi, @Eduardo Marques 

You have to use Behaviour (it works with issue creation screens), not Validator, that works on transition. 

Just yesterday I answered on similar question. Take a look please:

https://community.atlassian.com/t5/Jira-Software-questions/I-want-to-make-two-simple-text-fields-required-when-the-value-of/qaq-p/2386200

Eduardo Marques
Contributor
June 13, 2023

Thank you Evgeniy.

I removed the parts I did not need and I was able to make it work with the following code:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

FormField affectedField = getFieldById(getFieldChanged())
def affectedVal = affectedField.value
FormField gvField = getFieldByName("Report Date & Time")


if (affectedVal == "E-mail") {
gvField.setRequired(true)
} else {
gvField.setRequired(false)
}

 

Perfect

Like Evgenii likes this
1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2023

Hi @Eduardo Marques

For your requirement, you will need to use a Server-Side Behaviour for Field A.

Below is a sample code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def fieldA = getFieldById(fieldChanged)
def fieldAValue = fieldA.value.toString()

def fieldB = getFieldByName('Field B') // rename the field accordingly

if (fieldAValue == 'Option A') {
fieldB.required = false
} else if (fieldValue in ['Option B', 'Option C']) {
fieldB.required = true
}
 

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

The code above should meet your requirement. If Field B is required and the value for it is not set, a validation error will be displayed. 

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram 

Eduardo Marques
Contributor
June 13, 2023

Hi Ram,

Thank you for your reply.  I tested it also and it works. There was a typo in the else if and I correct so it works perfectly.

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def fieldA = getFieldById(fieldChanged)
def fieldAValue = fieldA.value.toString()

def fieldB = getFieldByName('Report Date & Time') // rename the field accordingly

if (fieldAValue == 'JIRA') {
   fieldB.required = false
} else if (fieldAValue in ['E-mail', 'Phone Call']) {
   fieldB.required = true
}

Suggest an answer

Log in or Sign up to answer