scriptrunner formfield

Lance Wong August 22, 2017

i want to remove an option from a select list (for example, if user isnt a manager, dont show option X)

can i do this using behaviors and the FormField class?

also, i can seem to find any API for the groovy-jira class "FormField"

2 answers

2 accepted

1 vote
Answer accepted
Lance Wong August 22, 2017

hello,

thanks for the response. Forget my "manager condition". What im really having trouble with is to be able to remove an option from a select list.

also, ive been to those pages and they have some specific examples of using FormField but there is no form API page (such as this https://docs.atlassian.com/jira/6.4/)

Aidan Derossett _Adaptavist_
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.
August 22, 2017

Unfortunately there isn't currently any published API for Behaviours or the plugin's classes. If you'd like some auto-completion for coding purposes, you could put this at the top of your code to access all of the FormField methods:

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

Aside from that, you could unzip the ScriptRunner executable jar file and take a look at the Behaviour classes themselves. Additionally, there is some documentation provided below the inline script box when editing a serverside script.

As for removing options from select lists, the process is a bit complex, but defintely doable. 

I've written and tested an example here that has worked for me to set the field options. If something isn't clear in the code, don't be shy to ask questions:

import com.atlassian.jira.component.ComponentAccessor

def cfm = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def formField = getFieldById(getFieldChanged())
//Custom Field version of the form field
def customField = cfm.getCustomFieldObjectByName("Custom Field Name")
def fieldConfig = customField.getRelevantConfig(issueContext)
def cfOptions = optionsManager.getOptions(fieldConfig)

//options that you want to be available to choose from
//Just the values, not the keys/IDs
def optionSet = ["Option 2","Option 3","Option 4"] //Example options

//Retrieve all of the options that you want and create a map of their ids and values.
//Set the field using those maps
formField.setFieldOptions(cfOptions.findAll{it.value in optionSet}.collectEntries{[(it.optionId) : it.value]})
Lance Wong August 28, 2017

yes, ive previously worked with select fields and their options. I was just unsure as to exactly how to deal with this with a formfield. i will try it. thanks

1 vote
Answer accepted
Aidan Derossett _Adaptavist_
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.
August 22, 2017

Hey Lance!

This shouldn't be too hard to accomplish with Behaviours. The best place to look for use of the API would probably be through the ScriptRunner documentation here & here.

But, if you elaborate just a little for me I can come up with a working example for you! :)

The case that you mentioned was if the "user isn't a manager." What do you mean by this? Do you mean if the user isn't a project manager, or some other custom project role or JIRA group? 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events