I am trying to use ScriptRunner to hide values ( in a dropdown) based on another field.

Jacob Francois May 4, 2021

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def formField = getFieldByName("Closed Reasons") // update FIELD_NAME with your custom field name
def customField = customFieldManager.getCustomFieldObject(formField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

def Case = getFieldByName("Case Type")

// update below OPTION_ONE, OPTION_TWO, OPTION_THREE with desired options
// note the options listed need to first be configured within the full project field context and must match exactly


if ( Case.getValue() == "Query")
{

def optionsMap = options.findAll {
it.value in ["User Management", "Password Reset", "Hierarchy Management", "Score Change", "Usability Query","Unsubscribe", "Additional Comments", "Customer could not find store/offer code", "Location Not Participating", "Data Import","Contact customer service/corporate #", "Duplicate","No Longer a Client", "Spam"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]

}
formField.setFieldOptions(optionsMap)

}
else if ( Case.getValue() == "Error")
{

def optionsMap = options.findAll {
it.value in ["Link Error", "Survey Error", "Reporting Error", "Platform Error", "Platform Outage","Speed Issue (Reporting)", "Speed Issue (Platform)", "File Processor Error", "Survey Timed Out", "User Computer Error","User Error", "Customer Did Not Recieve Incentive","Incorrect Information", "Spam","Survey Automatically Submitted","Coupon/Sweep Questions", "Duplicate", "No Longer a Client"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]

}
formField.setFieldOptions(optionsMap)


}
else if (Case.getValue() == "Appeal")
{

def optionsMap = options.findAll {
it.value in ["Appeal Approved", "Appeal Declined", "Raised In Error", "Duplicate", "No Longer a Client","Spam"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]

}
formField.setFieldOptions(optionsMap)

}
else if (Case.getValue() == "Change Request")
{

def optionsMap = options.findAll {
it.value in ["Change Requested", "Change Actioned", "Change Denied", "Duplicate", "No Longer a Client","Spam"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]

}
formField.setFieldOptions(optionsMap)


}
else if (Case.getValue() == "PII Request ")
{

def optionsMap = options.findAll {
it.value in ["GDPR Request Completed", "CCPA PII Request Completed", "Email Not Verified", "Duplicate","Spam"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]

}
formField.setFieldOptions(optionsMap)

}

else
{


}

3 answers

1 accepted

2 votes
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.
May 4, 2021

Hi @Jacob Francois 

For your requirement, I would suggest using a Server-Side Behaviour for the first list, which will determine the filter for the second list.

Below is a print screen of the Behaviour configuration:-

behaviour_config.png

Below is the sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName("List 2")

def optionManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)
def list2Config = list2CustomField.getRelevantConfig(issueContext)
def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions

if(list1Value == "Zone 1") {
listBNewOptions = ["USA", "Canada", "Mexico", "Brazil", "Argentina"]
} else if(list1Value == "Zone 2") {
listBNewOptions = ["UK", "Germany","Sweeden","UAE"]
} else if(list1Value == "Zone 3") {
listBNewOptions = ["India", "Malaysia","Singapore", "Thailand"]
} else {
listBNewOptions = ["USA", "Canada", "Mexico", "Brazil", "Argentina", "UK", "Germany","Sweeden","UAE", "India", "Malaysia","Singapore", "Thailand"]
}


def listBNewOptionsMap = [null: "None"]

listBNewOptionsMap += list2Options?.findAll {

it.value in listBNewOptions
}?.collectEntries {
[ (it.optionId.toString()) : it.value ]
}

list2.setFieldOptions(listBNewOptionsMap)


Please note, this sample code is not 100% exact to your environment. Hence you will need to modify it accordingly.

If you notice in the code I am using:-

def list1 = getFieldById(fieldChanged)


It ensures that the behaviour will trigger only if the first list, i.e. List 1, is updated. In this case, whenever the option in List 1 is updated, List 2 will update accordingly.

Below are a few test print screens:-

First, when no option is selected from List 1, i.e. None, List 2 will display all the options as shown below.

all_options.png

If the option in List 1 is updated to Zone 1, List 2 will only show the options associated with Zone 1, as shown below.

zone1_options.png

 If the option in List 1 is updated to Zone 2, List 2 will only show the options associated with Zone 2, as shown below.

zone2_options.png

And finally,  if the option in List 1 is updated to Zone 3, List 2 will only show the options associated with Zone 3, as shown below.

zone3_options.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Jacob Francois May 5, 2021

@Ram Kumar Aravindakshan _Adaptavist_  - Thank you. Does this work when the second dropdown is later in the workflow ? / on a pop up screen when the issue is being transitioned. 

Jacob Francois May 5, 2021

@Ram Kumar Aravindakshan _Adaptavist_  - Also the first field isn't on the create screen but part fo the workflow as well. 

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.
May 5, 2021

Hi @Jacob Francois

To clarify, are both the First List and Second List on the same transition screen?

If so, you can use the same code with a minor modification, i.e. include an additional if condition outside as shown below:- 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

def list2 = getFieldByName("List 2")

def optionManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)
def list2Config = list2CustomField.getRelevantConfig(issueContext)
def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions

if(actionName == "The Transition Screen Name") {
if(list1Value == "Zone 1") {
listBNewOptions = ["USA", "Canada", "Mexico", "Brazil", "Argentina"]
} else if(list1Value == "Zone 2") {
listBNewOptions = ["UK", "Germany","Sweeden","UAE"]
} else if(list1Value == "Zone 3") {
listBNewOptions = ["India", "Malaysia","Singapore", "Thailand"]
} else {
listBNewOptions = ["USA", "Canada", "Mexico", "Brazil", "Argentina", "UK", "Germany","Sweeden","UAE", "India", "Malaysia","Singapore", "Thailand"]
}

def listBNewOptionsMap = [null: "None"]

listBNewOptionsMap += list2Options?.findAll {
it.value in listBNewOptions
}?.collectEntries {
[ (it.optionId.toString()) : it.value ]
}

list2.setFieldOptions(listBNewOptionsMap)

}


Else, if they are in two different transition screens, the behaviour only may not be the best approach.

I hope this helps to answer your question. :)

 

Thank you and Kind Regards,

Ram

0 votes
Erik Buchholz
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.
May 5, 2021

Hi @Jacob Francois 

Can you explain, why you don't use simple cascading options?

I can imagine that the reason got lost at simplifying the problem. On the other hand this possibility would be much easier than using scriptrunner if it fulfills your requirements.

Regards

Jacob Francois May 5, 2021

@Erik Buchholz - the two custom fields comes up at different part of the workflow. 

0 votes
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2021

Hi @Jacob Francois 

working script ,, use behaviour..

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("Testing Field")
def typeOfEngagement = getFieldByName("Client")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Client")
def verticalValue = vertical.getValue()
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null
if (verticalValue.toString() == "A"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Google", "Microsoft"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Yahoo"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2021

Kindly close allowed option at end with )

Suggest an answer

Log in or Sign up to answer