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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to make a field required based on conditions in JSD

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

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?

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.

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!")
}

 

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')
}

 

 

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')
}

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

Clip2net_180213184840.png

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

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.

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?

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!")
}

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

Hello, thanks for your brilliant response!!

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
Community showcase
Published in Marketplace Apps & Integrations

Bitbucket Smart Commits vs. Genius Commits - What's the difference?

If you already heard about Smart Commits in Bitbucket, know that you just stumbled upon something even better (and smarter!): Genius Commits by Better DevOps Automation for Jira Data Center (+ Server...

106 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you