How to check the second value of a cascading select list in a custom field

Amir Katz (Outseer)
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.
March 27, 2018

I have a custom field, of type cascading select list, with these values (first -> second):

  customer_A - Location1      (one choice)

  customer_B - Location2      (two choices)

                      - Location3

etc.

However, when I select Customer_A in the first drop-down, the second drop-down has None and Location1.

Similarly, choosing Customer_B, I get 3 choices: None, Location2, Location3.

I tried to add a validator on a transaction, using a Simple Scripted Validator from the ScriptRunner add-on.

I tried the following 3 methods (as suggested in various posts), but none works:

cfValues['customfield_17637']?.values()[1].value != Null

cfValues.get('customfield_17637')?.keySet()?.size() == 2    // values are returned as map

cfValues['customfield_17637']?.values()*.value.size() == 2

A co-worker suggested to add an actual value of None and check against it, but I hate this because the user will see 2 None values.

Any idea?

 

 

 

 

3 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
March 29, 2018

Hey Amir, 

Try something like

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

def issue = issue as MutableIssue
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("CascadingSelect")
def cfValue = issue.getCustomFieldValue(cf) as HashMap

def parentOption = cfValue.values().getAt(0)?.value
def childOption = cfValue.values().getAt(1)?.value

if (!childOption) {
throw new InvalidInputException("Child option is Null")
}

Let me know if this does the trick

Aidan Derossett _Adaptavist_
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.
March 29, 2018

Heads up to Amir: 

Thanos's solution uses a Custom Validator and not a Simple Scripted Validator.

:D

Amir Katz (Outseer)
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.
April 2, 2018

Thanks to @Thanos Batagiannis [Adaptavist] @Aidan Derossett _Adaptavist_ - it works.

I made a minor modification so that the field name is a constant that can be used more than once:

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

def issue = issue as MutableIssue
final myField = "Account Name and Region"
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName(myField)

def cfValue = issue.getCustomFieldValue(cf) as HashMap

def parentOption = cfValue.values().getAt(0)?.value
def childOption = cfValue.values().getAt(1)?.value

if (!childOption) {
throw new InvalidInputException("Error in field '" + myField + "': the second part (region) cannot be empty")
}
0 votes
Amir Katz (Outseer)
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.
April 25, 2018

Apparently, something changed in ScriptRunner 5.3.7 and later - the hashMap trick no longer work.

Here is what works  for me (found it here: LINK)

 

Map cfValue_acct = issue.getCustomFieldValue(cf_acct) as Map

if (cfValue_acct) {
String acct_acct_name = cfValue_acct.get(null) // get 1st value
String acct_saas_region = cfValue_acct.get("1") // get 2nd value
// do what you want here...
}

 

 

0 votes
Sergio Palacio April 24, 2018

HI people I'm having errors using this solution.

Captura.PNG

Amir Katz (Outseer)
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.
April 24, 2018

please show the full script. Lines 9-10 are obscured

Sergio Palacio April 24, 2018

Captura2.PNG

Amir Katz (Outseer)
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.
April 24, 2018

The code works for me. Maybe you're using an older versions? I'm using ScriptRunner  5.3.7 on Jira 7.4.1

Amir Katz (Outseer)
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.
April 25, 2018

The error reported above is now happening to me as well, after my ScriptRunner version was updated to 5.3.7. I assume it worked before on an older version that I was using.

If anyone from Adaptavist is watching this thread, please respond.

Sergio Palacio April 25, 2018

I'm using the 5.3.9 version.

Suggest an answer

Log in or Sign up to answer