How to make a field required based on conditions in JSD

Paul Heer February 12, 2018

I need some help in creating a Behaviour for Jira Service Desk.  Basically, what I want to do is make a field required if the value of two other fields meet certain criteria.

Environment:

  • Jira Core 7.5.2
  • Jira Service Desk 3.8.3
  • Adaptavist ScriptRunner for JIRA 5.3.1

Fields:

  • Field1 - Radio Button field
  • Field2 - Select List (multiple choices)
  • Field3 - Radio Button field

Logic:

On creation, if Field1 = X and Field2 contains Y then set Field3 to required, else set Field3 to optional.

1 answer

1 accepted

1 vote
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.
February 13, 2018

Hi Paul,

I think a scripted validator is the solution here. But before I can proceed can you please clarify what is the condition for your multi-select list field that will make Field3 required? A specific set of options selected? Or at least one specific option selected?

Paul Heer February 13, 2018

The problem I have with a validator is that they don't work with Jira Service Desk from the customer's portal.

Field3 just needs to have any one of the options selected.

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.
February 13, 2018

That actually depends on how you write your validator. 

In any case, if I understood your requirement correctly, then if BOTH Field1 AND Field2 have ANY value, then make Field3 mandatory. 

Try this validator, it should work even with your customer portal:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def cfMgr = ComponentAccessor.getCustomFieldManager()
def cf1Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field1"))
def cf2Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field2"))
def cf3Value = issue.getCustomFieldValue(cfMgr.getCustomFieldObjectByName("Field3"))

if (cf1Value != null && cf2Value != null && cf3Value == null){
throw new InvalidInputException(cfMgr.getCustomFieldObjectByName("Field3").getName(), "Field3 is required!")
}

 

Paul Heer February 13, 2018

I stand corrected. The validator does work in JSD, but it doesn't actually display the error message in the portal.  So, it prevents the issue from being corrected, but it doesn't tell the customer why.

Here is the validator that I setup.  It works exactly as expected within JSD, it just doesn't display the error message in the portal.

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field1'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field2'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field3'))

if (custom1.value.contains('X') && custom2.value.contains('Y') && (!custom3)) {
throw new InvalidInputException(customFieldManager.getCustomFieldObjectByName('Field3').getName(), 'Required')
}

 

 

Paul Heer February 13, 2018

Thanks, Ivan.  The code works, but it doesn't display the error message on the customer's portal.

Here is the code I used:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field1'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field2'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Field3'))

if (custom1.value.contains('X') && custom2.value.contains('Y') && (!custom3)) {
throw new InvalidInputException(customFieldManager.getCustomFieldObjectByName('Field3').getName(), 'Required')
}
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.
February 13, 2018

That's odd. I've just tested this in my instance an error message pops just fine:

Clip2net_180213184840.png

Paul Heer February 13, 2018

Which browser are you using?  I've tried on both Chrome and Safari and the message doesn't appear on either.

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.
February 13, 2018

I'm on Windows so I can't say anything for Safari, but I've tested this successfully on the latest Chrome, MS Edge and Firefox 57.0 (64-bit). No problems.

Paul Heer February 13, 2018

I just upgraded my Chrome to the latest version and still not seeing the message.  Just to confirm, the code sample I pasted above is working on your instance?

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.
February 13, 2018

Not exactly, 

I'm using a much simpler code to debug InvalidInputException specifically:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("VIP")
if (issue.getCustomFieldValue(cf) == null){
throw new InvalidInputException(cf.getName(),"Field is required!")
}
Paul Heer February 14, 2018

For posterity, we found that JSD was not returning the field name, but the format "customfield_XXX".  Here's the code we eventually got working in both JSD and on the portal:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def custom1 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldA'))
def custom2 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldB'))
def custom3 = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('fieldC'))
def custom4 = "customfield_" + customFieldManager.getCustomFieldObjectByName('fieldC').getIdAsLong().toString()

if (custom1.value.contains('X') && custom2.value.contains('Y') && custom3 == null) {
throw new InvalidInputException(custom4, 'Error Message')
}

Thanks for your help, @Ivan Tovbin

Like # people like this
Roger Vilagut March 14, 2019

Hello, thanks for your brilliant response!!

Marcela Junyent
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.
September 7, 2020

Hello @Ivan Tovbin  can you tell me where do I use this code if is not a behavoir?

 

Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events