Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

how to restrict field values based on another field values selection

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

0 votes
Answer accepted
Antoine Berry Community Leader 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

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 Jun 03, 2019

Hi @Musku Nagaraju ,

My bad, Line 20 should be : 

if (instanceValue == "Dev"){

Let me know if that fixed the error.

Antoine

Hi @Antoine Berry 

Thanks Berry, It is working fine for me.

 

Thanks

Nagaraju

Like Antoine Berry likes this

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 Jun 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") { ...

thanks @Antoine Berry . the first option worked for me

@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

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

Like Antoine Berry likes this

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?

Can you please some one help me on this?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

321 views 3 14
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you