Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

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

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

@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. 

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

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

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

Kindly close allowed option at end with )

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

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