Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Behaviour - auto select list based on other list selection not working

Ronny June 16, 2018

Basically I have 3  custom field lists:

Say they are :

ListA: A,B,C

ListB: AA, BB, CC

ListC: 1,2,3

 

I want ListC Value to be auto set based on the user selection of ListA and ListB.

So for example if user select A in ListA and AA in ListB, then listC should be set to 3.

 

Below are the codes that Im working with:, I think I have some error somewhere.

----------------------------------------------------------

import com.atlassian.jira.component.ComponentAccessor

 

// Get the Custom Field Manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def selectList1 = getFieldById("customfield_14204") //Consequence Rating
def selectList2 = getFieldById("customfield_14205") // Likelihood Rating
def selectList3 = getFieldById("customfield_14214") // Risk Level


// Get the value of selectList1
def selectList1Val = selectList1.getValue()
def selectList2Val = selectList2.getValue()
def selectList3Val = selectList3.getValue()


//Consequence Rating : Extreme
if(selectList1Val.toString().contains("Extreme"))
{
// Check if Likelihood Rating is : Almost Certain
if(selectList2Val.toString().contains("Almost Certain"))
{
selectList3.setFormValue("Critical")
}
}

3 answers

1 accepted

1 vote
Answer accepted
Mark Markov
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.
June 16, 2018

Hello @Ronny

You need set behaviour set on field select list 2 and select list 1, and put code like this on both(change first two line depends on the field where you put code):

def selectlist2 = getFieldById(getFieldChanged()) //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist1 = getFieldByName("Selectlist 1") // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("Selectlist 3")
if ("AA".equals(selectlist2.getValue()) && "A".equals(selectlist1.getValue())){
selectlist3.setFormValue("1")
}
if ("BB".equals(selectlist2.getValue()) && "B".equals(selectlist1.getValue())){
selectlist3.setFormValue("2")
}
if ("CC".equals(selectlist2.getValue()) && "C".equals(selectlist1.getValue())){
selectlist3.setFormValue("3")
}

You dont need customfieldmanager here because you didnt get info from issue. 

Mark Markov
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.
June 18, 2018

Hello @Ronny

Sorry for long response.

Yes, you ve got the point about what and where replace in my example.

But my mistake, i forget that select list have option value to set, so right code bellow:

for selectlist1

import com.atlassian.jira.component.ComponentAccessor

def selectlist2 = getFieldByName("selectlist2") //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist1 = getFieldById(getFieldChanged()) // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("selectlist3")
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("ListC")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if ("AA".equals(selectlist2.getValue()) && "A".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "1" }
selectlist3.setFormValue(optionToSelect.optionId)
}
if ("BB".equals(selectlist2.getValue()) && "B".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "2" }
selectlist3.setFormValue(optionToSelect.optionId)
}
if ("CC".equals(selectlist2.getValue()) && "C".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "3" }
selectlist3.setFormValue(optionToSelect.optionId)
}

 for selectlist2

import com.atlassian.jira.component.ComponentAccessor

def selectlist2 = getFieldById(getFieldChanged()) //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist1 = getFieldByName("selectlist1") // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("selectlist3")

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectByName("ListC")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if ("AA".equals(selectlist2.getValue()) && "A".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "1" }
selectlist3.setFormValue(optionToSelect.optionId)
}
if ("BB".equals(selectlist2.getValue()) && "B".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "2" }
selectlist3.setFormValue(optionToSelect.optionId)
}
if ("CC".equals(selectlist2.getValue()) && "C".equals(selectlist1.getValue())){
def optionToSelect = options.find { it.value == "3" }
selectlist3.setFormValue(optionToSelect.optionId)
}

And you dont need initialiser function. It runs only once when form loads.

Ronny June 22, 2018

Thanks @Mark Markov it works Yay.

 

I also try to check if the list value is null (None). Basically I want the list to check if any of the selected list value is set to None, Then the selectlist3  value need to be set to None/null

eg.

selectlist1 = None && selectlist2= 1

selectlist3 become None

 

or

selectlist1 = 1 && selectlist2= None 

selectlist3 become None

or

selectlist1 = None && selectlist2= None 

selectlist3 become None

 

 

 

Currently I tried the below code but does not work:

 

...

if ("".equals(selectlist1.getValue()) || "".equals(selectlist2.getValue())){
def optionToSelect = options.find { it.value == "" }
selectlist3.setFormValue(optionToSelect.optionId)
}

...

Mark Markov
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.
June 22, 2018

with None value its easier. As you say none value is null and code must be like this:

if (selectlist2.getValue()==null && selectlist1.getValue()==null){
selectlist3.setFormValue(null)
}
Ronny June 23, 2018

ThAnk YOu !!!!

it works !! 

 

:)

Mark Markov
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.
June 23, 2018

You re welcome!

If it hepls you please mark answer as accepted :)

0 votes
Ronny June 16, 2018

I'm a bit confuse with the below :

 

def selectlist2 = getFieldById(getFieldChanged()) //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist1 = getFieldByName("Selectlist 1") // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("Selectlist 3")

 

so since I will need this script added on both selectlist 1 and selectlist 2, does that mean that:

 

For selectlist1 I have to add the below :

def selectlist1 = getFieldById(getFieldChanged()) //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist2 = getFieldByName("Selectlist 2") // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("Selectlist 3")
...

 

For selectlist2 I have to add the below :

 

def selectlist2 = getFieldById(getFieldChanged()) //for select list1 must be def selectlist2 = getFieldByName("selectlist2")
def selectlist1 = getFieldByName("Selectlist 1") // for select list 1 must be def selectlist1 = getFieldById(getFieldChanged())
def selectlist3 = getFieldByName("Selectlist 3")
...

 

 

I have tried the above and the value for selectlist3 still unchanged. :(

0 votes
Ronny June 16, 2018

Hi @Mark Markov,

thank you for helping me :)

 

Im quite new to scriptrunner so please excuse my silly questions.

 

So what I need to is to put that script on both list1 and lisr2 but not list3?

How about initialiser function?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events