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")
}
}
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You re welcome!
If it hepls you please mark answer as accepted :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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. :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.