Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Organizations field display value on issue

Florian Edian
Contributor
February 4, 2022

Hi guys,

I'm currently trying to make a little script to help me link issues between JSM projects.

One part of my script is to get the correct batch of issues to link to my targeted issue.

In order to do this, I'm running a JQL request and I want a dynamic value in it regarding the Organizations field.

It seems like my current code is working as I'm logging the correct value. However I get a static type checking error and I would like to know how I could get ride of it?

Thanks for your help,

PS : already did a ton of researches ;)

def issueManager = ComponentAccessor.getIssueManager()
def currentIssue = issueManager.getIssueObject("SBDDS-177")
def cfm = ComponentAccessor.getCustomFieldManager()
def cfOrg = currentIssue.getCustomFieldValue(cfm.getCustomFieldObject("customfield_11608"))
log.warn (cfOrg)
def cfValue = cfOrg.name[0]
log.warn (cfValue)

def jql = jqlParser.parseQuery("Organisations = \"${cfValue}\" AND statusCategory != Done")

2022-02-04 15:41:41,617 WARN [runner.ScriptBindingsManager]: [CustomerOrganizationImpl{id=588, name=ACME}]
2022-02-04 15:41:41,617 WARN [runner.ScriptBindingsManager]: ACME

2022-02-04 15_51_19.png

1 answer

1 accepted

3 votes
Answer accepted
PD Sheehan
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.
February 4, 2022

If you run this in your console,

import com.atlassian.jira.component.ComponentAccessor
def issue = ComponentAccessor.issueManager.getIssueObject('KEY-123')
def orgCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Organizations')
issue.getCustomFieldValue(orgCf)*.getClass()

You should see this result:

[class com.atlassian.servicedesk.internal.feature.organization.model.CustomerOrganizationImpl]

So if you want to get rid of the static type checking, you can cast your value to that class (or its interface)

So just expanding on what I have up there, I can get the org(s) name without any static type checking errors:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.servicedesk.api.organization.CustomerOrganization
@WithPlugin("com.atlassian.servicedesk")
def issue = ComponentAccessor.issueManager.getIssueObject('KEY-123')
def orgCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('Organizations')[0]
def orgValues = issue.getCustomFieldValue(orgCf) as List<CustomerOrganization>
orgValues*.name

 The "as List<CustomerOrganization>" tells the static type checker what type to expect.

Bonus... you also get ctrl-space auto-complete.

Florian Edian
Contributor
February 7, 2022

Hi Peter-Dave,

Thanks a lot for your very didactic answer ;)

It worked and I even got a new shorcut!

Florian

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events