how to restrict field values based on another field values selection

Musku Nagaraju May 28, 2019

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

 

2 answers

1 accepted

2 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 31, 2019

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

Musku Nagaraju June 3, 2019

Hi @Antoine Berry 

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

 

hide value.PNG

 

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)
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 3, 2019

Hi @Musku Nagaraju ,

My bad, Line 20 should be : 

if (instanceValue == "Dev"){

Let me know if that fixed the error.

Antoine

Musku Nagaraju June 3, 2019

Hi @Antoine Berry 

Thanks Berry, It is working fine for me.

 

Thanks

Nagaraju

Like Antoine Berry likes this
idiyana June 24, 2020

Hi @Antoine Berry 

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?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 26, 2020

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") { ...
idiyana June 26, 2020

thanks @Antoine Berry . the first option worked for me

Inayat N January 7, 2021

@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

Melanie Nitter February 8, 2021

I want to do the same as Inayat Nahvi.. pls @Antoine Berry can you help with this?

Like Antoine Berry likes this
Inayat N February 10, 2021

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.

  • If MultiselectA = 1, I want MultiselectB to show ABC
  • If MultiselectA = 2, I want MultiselectB to show DEF
  • If MultiselectA = 3, I want MultiselectB to show GHI
  • If MultiselectA = 12, I want MultiselectB to show ABCDEF
  • If MultiselectA = 23, I want MultiselectB to show DEFGHI

 

Has anyone got this to work?

0 votes
Musku Nagaraju May 31, 2019

Can you please some one help me on this?

Amine Tanboura April 28, 2023

I tried with your script and I faced an error message.

Can you please help!

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

 

@BaseScript 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)
}
[Static type checking] - Cannot call com.onresolve.jira.groovy.user.FormField#setFieldOptions(java.util.Map <?, java.lang.String>) with arguments [java.util.Map <java.lang.Object, java.lang.Object>]
else {
number.setFieldOptions(options)
}
Inayat N April 28, 2023

Hi @Amine Tanboura 

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)
}

 

Amine Tanboura May 11, 2023

This line is the error message I am facing

I added it in this level in order to let you understand the issue.

Karl Samson August 15, 2023

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events