Missed Team ’24? Catch up on announcements here.

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

scriptrunner console: How to pull all option IDs for a specific customfield

Michael
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.
April 23, 2024

Hi all,

I'm trying to pull all option IDs from a specific multi-select customfield via Scriptrunner's console.

So far I have the following, but it just gives me "Null" as the output. Can someone please help?

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

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def selectBrandsField = getFieldById("customfield_11808")

log.warn(selectBrandsField)

selectBrandsField.getFieldOptions().each {println("$it.value")}

Thanks,

Mike

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 26, 2024

Hi Micheal,

It's impossible to get the ID of the options from a Multi-User picker by just getting the field options via ScriptRunner' Behaviour.

You need to first make a REST Endpoint to invoke the issue in which the Multi-User picker is added and filter the custom field, i.e. something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
fieldValueFromIssue { MultivaluedMap queryParams, body, HttpServletRequest request ->
def applicationProperties = ComponentAccessor.applicationProperties

def hostUrl = applicationProperties.getString('jira.baseurl')
def issueKey = queryParams.getFirst('issueKey')
def customFieldId = queryParams.getFirst('fieldId')

def username = 'admin'
def password = 'q'
def path = "/rest/api/2/issue/${issueKey}"

final def headers = ['Authorization': "Basic ${"${username}:${password}".bytes.encodeBase64()}", 'Accept': 'application/json'] as Map
def http = new RESTClient(hostUrl)

http.setHeaders(headers)
def resp = http.get(path: path) as HttpResponseDecorator

if (resp.status != 200) {
log.warn 'Commander did not respond with 200 for retrieving project list'
}

def issueJson = resp.data as Map

Response.ok(new JsonBuilder(issueJson['fields']["${customFieldId}"]['id']).toPrettyString()).build()
}

Below is a screenshot of the REST Endpoint configuration:-

rest_endpoint.png

Once this is added, you can then invoke the REST Endpoint via a Server-Side Behaviour for the Multi-Select field and pull the IDs of the Multi-Select field something like this:-

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

@BaseScript FieldBehaviours behaviours
def multiSelect = getFieldById(fieldChanged)
def multiSelectValue = multiSelect.value
def host = applicationProperties.getString('jira.baseurl')

if (underlyingIssue && multiSelectValue) {
def baseUrl = "${host}/rest/scriptrunner/latest/custom/fieldValueFromIssue?issueKey=${underlyingIssue.key}&fieldId=${multiSelect.fieldId}".toString()
log.warn "=======>>>>> ${baseUrl.toURL().text}"
}

Below is a screenshot of the Server-Side Behaviour configuration:-

behaviour_config.png

Please note that the sample working codes above are not 100% exact to your environment. Hence, you will need to make the required modifications.

If you observe the Server-Side Behaviour code above, it is pulling the data from the REST Endpoint for the Multi-Select field that is added to this issue.

Please note that this cannot be done on the create screen. It can only be done after the issue has been created, i.e. either on an Edit screen or a Transition screen.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

 

TAGS
AUG Leaders

Atlassian Community Events