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

Trying to call current field value of an insight custom field

Kurtis Schmitz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 21, 2022

Hi Everyone,

 

I'm a bit new to scriptrunner API Calls and Jira in general but I've been trying to set up a provisioning list and the long and the short of it is that I am able to call an Insight Custom field using the code below.

The issue I'm having is that it always returns the value as [Alabama (IAS-575858)] Which is a Concatenation of Alabama and the Key Value. Is there anyway just to get the Value of State Name or the Label Field? I've been reading documentation for days at this point and just can't figure it out. If I use a plain text field it works fine but Insight Objects don't work the same.

I would appreciate any insight (ba dum crash) on what I'm doing wrong here. 

2022-06-21 16_02_55-Insight - JIRA - SNAPSHOT April 20 2022.png

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.component.ComponentAccessor



IssueManager im = ComponentAccessor.getIssueManager()

MutableIssue issue = im.getIssueObject("IPT-88")


    def customFieldManager = ComponentAccessor.getCustomFieldManager()

    def USState = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15500"))

return USState

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave 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.
June 21, 2022

What you are getting is an ArrayList of  "objectBean" objects. In your case, there is a single value in that list.

The array list and the object bean happen to have some default string representation that causes the output to look like "[Alabama (IAS-575858)]"

If the attribute you want happens to be the Label (or if you're only interested in the key)you can get either with something like this:

import com.atlassian.jira.component.ComponentAccessor
def im = ComponentAccessor.issueManager
def issue = im.getIssueObject("IPT-88")
def customFieldManager = ComponentAccessor.gcustomFieldManager

def stateCf= customFieldManager.getCustomFieldObject("customfield_15500")
def cfValue = issue.getCustomFieldValue(stateCf)

def stateObject = cfValue[0]

def objectLabel = stateObject.label
def objectKey = stateObject.objectKey

 

But if you want to retrieve a value for a specific attribute (that isn't the label), then it gets a bit more complicated.

import com.atlassian.jira.component.ComponentAccessor

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
@WithPlugin('com.riadalabs.jira.plugins.insight')
@PluginModule ObjectTypeFacade objectTypeFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade

def im = ComponentAccessor.issueManager
def issue = im.getIssueObject("IPT-88")
def customFieldManager = ComponentAccessor.gcustomFieldManager

def stateCf= customFieldManager.getCustomFieldObject("customfield_15500")
def cfValue = issue.getCustomFieldValue(stateCf)

def stateObject = cfValue[0]

def attributeName = "State Name"
def objectType = objectTypeFacade.loadObjectType(stateObject.objectTypeId)
def attributeBean = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id).find{ it.name == attributeName}

def attributeValues = stateObject.objectAttributeBeans.find{it.objectTypeAttributeId == attributeBean.id}?.objectAttributeValueBeans*.value

//all attribtutes are stored as list, even when max cardinality = 1
//so we get the first one here
if(attributeValues){
def stateName = attributeValues[0]
}

I wrote an article with some examples: https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Interacting-with-Insight-objects-from-Scriptrunner-Server-Data/ba-p/1649857

This shows how it can be simplified with a utility class.

Kurtis Schmitz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 24, 2022

Thanks for your help! This is exactly what I was looking for and is far more reliable then the substring manipulation I was going to do instead haha.

Based on the things shown here I can actually put it all into one degenerate line of code.

def USState = (issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15500"))).label[0]


I'm not trying to call attributes other then the label at present, but I'm bookmarking this post in case I do later and will definitely take a look at your guide if that happens. 

Once again thanks a million for your assistance here!

TAGS
AUG Leaders

Atlassian Community Events