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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,462,699
Community Members
 
Community Events
176
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

Atlassian Community Events