Hi Team,
I am using script runner plugin in my organization, I am looking to restrict field values based on another filed values?
Like I have single select fields called "Instance" and values Dev, Production,
and I have one more select field called "Number" values 1, 2, 3 ,4 5, 6 ,7 ,8 9
When I select "Dev" value in Instance I need to see only 1, 3, 5 values in number filed.
If I select production I need to see all values in Number field.
can you help me with right script code to achieve this.
Thanks
Nagaraju
Hi @Musku Nagaraju ,
Create a behaviour and map it to the instance field. Use this script (update the custom field ids) :
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
int instanceId = 11001
def instance = getFieldById("customfield_" + instanceId)
def instanceValue = instance.getValue()
int numberId = 11002
def number = getFieldById("customfield_" + numberId)
def optionsManager = ComponentAccessor.getOptionsManager()
def numberCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(numberId)
def numberConfig = numberCustomField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(numberConfig)
if (instanceValue() == "Dev"){
def map = ["1","3","5"]
def optionsMap = options.findAll {
it.value in map // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
number.setFieldOptions(optionsMap)
}
else {
number.setFieldOptions(options)
}
Antoine
Thanks for your inputs on this, but this is not working for me I created behavior with instance fields.
also I am getting error in code, Please have a look and help me on this,
I am attaching screen shot and log file
Logs:
2019-06-03 13:14:32,988 http-nio-8887-exec-19 ERROR admin 794x711x3 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17501, file: <inline script> java.lang.NullPointerException: Cannot invoke method call() on null object at Script1.run(Script1.groovy:22) 2019-06-03 13:14:37,018 http-nio-8887-exec-24 ERROR admin 794x713x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:14:37,022 http-nio-8887-exec-24 ERROR admin 794x713x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:14:41,419 http-nio-8887-exec-11 ERROR admin 794x715x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:14:41,423 http-nio-8887-exec-11 ERROR admin 794x715x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:14:45,593 http-nio-8887-exec-25 ERROR admin 794x716x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:14:45,596 http-nio-8887-exec-25 ERROR admin 794x716x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:16:43,705 http-nio-8887-exec-1 ERROR admin 796x732x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:16:43,707 http-nio-8887-exec-1 ERROR admin 796x732x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> java.lang.NullPointerException: Cannot invoke method call() on null object at Script1.run(Script1.groovy:22) 2019-06-03 13:16:48,749 http-nio-8887-exec-14 ERROR admin 796x735x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:16:48,752 http-nio-8887-exec-14 ERROR admin 796x735x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:21:15,902 http-nio-8887-exec-4 ERROR admin 801x1027x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:21:15,904 http-nio-8887-exec-4 ERROR admin 801x1027x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> java.lang.NullPointerException: Cannot invoke method call() on null object at Script1.run(Script1.groovy:22) 2019-06-03 13:21:23,443 http-nio-8887-exec-10 ERROR admin 801x1030x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:21:23,445 http-nio-8887-exec-10 ERROR admin 801x1030x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:21:32,335 http-nio-8887-exec-3 ERROR admin 801x1031x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:21:32,340 http-nio-8887-exec-3 ERROR admin 801x1031x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22) 2019-06-03 13:22:55,371 http-nio-8887-exec-23 ERROR admin 802x1088x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:22:55,375 http-nio-8887-exec-23 ERROR admin 802x1088x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> java.lang.NullPointerException: Cannot invoke method call() on null object at Script1.run(Script1.groovy:22) 2019-06-03 13:23:02,423 http-nio-8887-exec-6 ERROR admin 803x1090x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] ************************************************************************************* 2019-06-03 13:23:02,427 http-nio-8887-exec-6 ERROR admin 803x1090x1 1uns9r7 0:0:0:0:0:0:0:1 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: TEST/Bug, user: admin, fieldId: customfield_17600, file: <inline script> groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: () values: [] Possible solutions: wait(), any(), wait(long), any(groovy.lang.Closure), take(int), each(groovy.lang.Closure) at Script1.run(Script1.groovy:22)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Musku Nagaraju ,
My bad, Line 20 should be :
if (instanceValue == "Dev"){
Let me know if that fixed the error.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have same objective to achieve. But the field that i use to control other field was multi select field. can you suggest the script?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @idiyana ,
The script would be close to the same. Instead of
if (instanceValue == "Dev"){ ...
You would have
if ("Dev" in instanceValue){ ...
or
if (instanceValue.contains("Dev") { ...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Antoine Berry , your script works great for me when both custom fields are single select. How about if the second custom field is multi select?
In my case single select customfieldA has values of 1, 2, 3, 4. CustomfieldB is a mutliselect with A, B, C, D, E, F ,G.
If single select A=1, I want multiselect B to show BCD
If single select A=2, I want multiselect B to show DEFG
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Melanie Nitter and @Antoine Berry , I did get my original scenario to work using the suggestion @Antoine Berry provided to @idiyana
I am now working in a scenario where both custom fields are multi-select. Let's say MultiselectA has options of 1,2,3. MultiselectB has options of A,B,C,D,E,F,G,H,I.
Has anyone got this to work?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried with your script and I faced an error message.
Can you please help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your code compiles for me if I remove the "[static type checking] ...." line. This compiles for me using Scriptrunner v8.0.0.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@@BaseScrip FieldBehaviours fieldBehaviours
int instanceId = 15950
def instance = getFieldById("customfield_15950")
def instanceValue = instance.getValue()
int numberId = 14953
def number = getFieldById("customfield_14953")
def optionsManager = ComponentAccessor.getOptionsManager()
def numberCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(numberId)
def numberConfig = numberCustomField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(numberConfig)
if (instanceValue == "Full"){
def map = ["2 Hours"]
def optionsMap = options.findAll {
it.value in map // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()) : it.value
]
}
// error message : number.setFieldOptions(optionsMap)
}
else {
number.setFieldOptions(options)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This line is the error message I am facing
I added it in this level in order to let you understand the issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, this is exactly the type of Field Restriction I'm trying to implement. Except that I need to make a custom field called 'Supports/Impacted Stakeholders' (Checkbox) 'Read Only', unless the custom field 'Task Type' (Single-select) is in 'Technical Assessment'.
Any help greatly appreciated, as I'm struggling with the structure of the code above!
Thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.