How to set options on select list based on values from two select lists using Behaviors?

Chander Inguva
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 12, 2018

Hi,

i could use Adaptavist Script Runner's Behaviors functionality to set a pick list based on a pick list value selected succesfully.

 

I am looking for another use case where i would like to set third pick list based on values from pick list 1 and pick list2 combined,

 

Say, if ((picklist1.value == "abc") && picklist2.value == "xyz")) {picklist3.value = "1234"}

2 answers

2 votes
Alexey Matveev
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 12, 2018

Could you provide the script for 2 pick lists? The script would be basically the same for 3 pick lists. I could modify it.

Chander Inguva
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 12, 2018

Hey Alexey,

 

Here is the working script to set other pick list(s) based on value of issue type.

 


import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def crField = getFieldByName("Change Request")
def formField = getFieldByName("Application Component")
def
customField = customFieldManager.getCustomFieldObject(formField.getFieldId()) // Pick list Field that dictates other fields on form based on its value
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)


def projectKey = getIssueContext().getProjectObject().getKey()
def issueTypeName = getIssueContext().getIssueType().getName()

def WSField = getFieldByName("Wiki Space Name")
def SKField = getFieldByName("Space Key")
def SAField = getFieldByName("Space Administrators")
def TJRField = getFieldByName("Type of JIRA Request")

//GIT - JIRA Integration Fields
def GLUField = getFieldByName("GitLab URL")
def AUNField = getFieldByName("Additional URL's / Notes")
def JPField = getFieldByName("JIRA Project")

if (issueTypeName == "SharePoint / Wiki Site Request")
{
def optionsMap = options.findAll {
it.value in ["Confluence", "SharePoint"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(true)


}

else if (issueTypeName == "JIRA Request")
{
def optionsMap = options.findAll {
it.value in ["Jira"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(true)



}

else if (issueTypeName == "Atlassian Plugin Request")
{
def optionsMap = options.findAll {
it.value in ["Confluence","Jira"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(true)
}


else if (issueTypeName == "Git Request")
{
def optionsMap = options.findAll {
it.value in ["Jira"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(true)

}

else if (issueTypeName == "Slack Request")
{
def optionsMap = options.findAll {
it.value in ["Jira"]
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(true)
}

else
{
def optionsMap = options.findAll {
it.value in ["SharePoint","Confluence","ePurchase", "eContract","Jira","O365","ADP","OrgChart","ServiceNow","Navision","Other"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
formField.setFieldOptions(optionsMap)
formField.setRequired(false)



}

 

P.S: This is an initialiser written using behaviours controlled based on issue type selected.

Goal: To use two picklists' values to control other picklist(s) 

 

Thanks for your help in advance.

 

Chander 

Chander Inguva
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 15, 2018

@A any suggestions  on this?

Alexey Matveev
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 15, 2018

I am not from Adaptivist but I guess, you would need to add in your code something like this:

def picklist1 = getFieldByName("Pick List 1") 
def picklist2 = getFieldByName("Pick List 2")
def picklist3 = getFieldByName("Pick List 3")

if (picklist1.getValue() == "abc" && picklist2.getValue() == "xyz") {
picklist3.setFormValue() = "1234"
}
Chander Inguva
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 16, 2018

Hey @Alexey Matveev,

 

No luck with that

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def picklist1 = getFieldById("customfield_14004") //Severity
def picklist2 = getFieldById("customfield_13800") // Frequency
def picklist3 = getFieldById("customfield_17901") //Biz Priority

def customField = customFieldManager.getCustomFieldObject(picklist3.getFieldId()) // Pick list Field that dictates other fields on form based on its value
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (picklist1.getValue() as String == "Critical" && picklist2.getValue() as String == "Hot Executive Escalation") {
def optionsMap = options.findAll {
it.value in ["High"] // list of options you want to show on Biz Priority Field
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
picklist3.setFieldOptions(optionsMap)


}

Screen Shot 2018-02-16 at 8.48.48 AM.png

You can see Biz Priority on Create Issue screen is still showing "None" instead of "High"

Alexey Matveev
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 16, 2018

Do you want to set a value for the Biz Prority or to restrict available options? In your code you restrict available options. You are not setting a value. To set a value you should use 

 picklist3.setFormValue(OptionId)

You can find a working example here:

https://community.atlassian.com/t5/Marketplace-Apps-questions/jira-scriptrunner-set-form-value-for-select-single-choice/qaq-p/621402 

Chander Inguva
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 17, 2018

@Alexey Matveev

I would like to set the value, but my question is where should we write this server side script?

 

Can you show me working example on initialiser?

 

i tried adding initialiser, but it never updates third pick list based on selected values of picklist 1 and picklist 2

import com.atlassian.jira.component.ComponentAccessor

//Severity, Frequency and Biz Priority Fields
def sevField = getFieldByName("Severity")
def fieldB = getFieldByName("Frequency") // ("customfield_13800")
def fieldC = getFieldByName("Biz Priority")


def sevselectedOption= sevField.getValue() as String
def freqselectedOption = fieldB.getValue() as String
if(sevselectedOption.contains("Critical") && freqselectedOption.contains("Hot Executive Escalation")){

fieldC.setFormValue(18311)
}

else if (sevselectedOption.contains("Critical") && freqselectedOption.contains("Often")){
fieldC.setFormValue(18311)
}
else if (sevselectedOption.contains("Critical") && freqselectedOption.contains("Occasional")){
fieldC.setFormValue(18313)
}
else if (sevselectedOption.contains("Critical") && freqselectedOption.contains("Rare")){
fieldC.setFormValue(18313)
}
else if (sevselectedOption.contains("Major") && freqselectedOption.contains("Hot Executive Escalation")){
fieldC.setFormValue(18311)
}
else if (sevselectedOption.contains("Major") && freqselectedOption.contains("Often")){
fieldC.setFormValue(18311)
}

else if (sevselectedOption.contains("Major") && freqselectedOption.contains("Occassional")){
fieldC.setFormValue(18313)
}

else if (sevselectedOption.contains("Major") && freqselectedOption.contains("Rare")){
fieldC.setFormValue(18313)
}
else if (sevselectedOption.contains("Minor") && freqselectedOption.contains("Hot Executive Escalation")){
fieldC.setFormValue(18311)
}
else if (sevselectedOption.contains("Minor") && freqselectedOption.contains("Often")){
fieldC.setFormValue(18313)
}
else if (sevselectedOption.contains("Minor") && freqselectedOption.contains("Occassional")){
fieldC.setFormValue(18313)
}
else if(sevselectedOption.contains("Minor") && freqselectedOption.contains("Rare")){
fieldC.setFormValue(18313)
}
else{
fieldC.setFormValue(18312)
}

Can you help me spot what i am doing wrong here?

 

if it is not to place serverside script on initialiser, how do we place scripts on picklist fields to achieve this? 

Alexey Matveev
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 18, 2018

It is not on the initialiser. You must add Severity and Frequency fields to the behaviour and put the script for the both fields.

Chander Inguva
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 19, 2018

Thanks Alexey,

That did the trick. Thanks for pointing at right direction and for your prompt response.

 

Cheers

Chander

Tennille Noach May 17, 2018

Hi @Chander Inguva

I am just trying to achieve a small part of what you had in your query - I want a custom field (called "Required Item/s") to display certain values based on the "Issue type" selected.   

What script can I use to achieve that?

Do I create the new Behaviour and then do I need to add the script as an Initialiser?

Or do I have to add the 'Issue Type' field into the Behaviour and then add the script as a Validator for that field?

Or do have to add both the 'Issue Type' field and the 'Required Item/s' field into the Behaviour and then add script as a Validator for one or both?

I'm uncertain.  I've tried a few things and can't seem to get the custom field values to change when I select different issue types in that project, the whole listr of values keeps appearing.

 

I'd love some help, thanks!

0 votes
KRC July 14, 2021

hi @Alexey Matveev 

Instead of this

if (picklist1.getValue() == "abc"

 How can i write based on option id or value id of abc, reason is that, when abc changes to XYZ, i mean when option gets edited, instead of adding new option, I can leverage option id.

I want to implement in my code here

import com.atlassian.jira.component.ComponentAccessor

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


def Field1 = getFieldByName("Failed Rule");
def Field2 = getFieldByName("Failed ID");


def Field1CF = customFieldManager.getCustomFieldObjectsByName("Failed Rule")[0]
def config = Field1CF.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

def Field2CF = customFieldManager.getCustomFieldObjectsByName("Failed Rule ID")[0]
def config2 = Field2CF.getRelevantConfig(getIssueContext())
def options2 = optionsManager.getOptions(config)




def cF= getFieldById("customfield_10292").getValue();
if (cF.toString().trim().equalsIgnoreCase("abc"))


{
Field1.setRequired(true)
Field2.setRequired(true)
}

else {
Field1.setRequired(false)
Field2.setRequired(false)
}



Can you share your insights plz?

Suggest an answer

Log in or Sign up to answer