Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,588
Community Members
 
Community Events
165
Community Groups

Trying to call current field value of an insight custom field

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

1 vote
Answer accepted

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.

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!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

203 views 0 5
Join discussion

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