Working with Insight object's attributes using JMWE (Server/DC) and Groovy
Problem
Customers might need to pull a specific Insight's object attribute info. While the issue.get("<customfield_id>")?.first()?.getInsightAttributeValue("<attribute_name>")
code snippet can help in some cases, if the custom field references another Insight object instead of a regular value, the id will be returned instead of the actual value.
Refer to the example below:
Solution
Use Groovy and the Insight-related classes to explore and drill down into an Insight field object attributes:
def objectFacade = getComponent(com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade) def objId = issue.get("<custom_insight_field_id>").first().getInsightAttributeValue("<object_attribute>") objectFacade.loadObjectBean(objId)?.name
As you can see in the recording below, we have managed to get the “Server Owner” attribute value (not just the ID shown in this article's Problem section), even when it references another Insight object.
The code snippet provided earlier can vary depending on the Insight object attribute type. For example, if we multiple values for an “Approvers” Insight object attribute that refers to a User’s property type from another Insight object (refer to the captures below), then the code snippet will look as follows:
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
def objectFacade = getComponent(ObjectFacade)
def objIds = issue.get("<custom_insight_field_id>").first().getInsightAttributeValue("<object_main_attribute>")
def names_str = ""
def userManager = ComponentAccessor.getUserManager()
for (i in objIds) {
def jiraUsrId = objectFacade.loadObjectBean(i).getInsightAttributeValue("<object_main_attribute>")
names_str = names_str + userManager.getUserByKey(jiraUsrId).displayName + " "
}
return names_str.trim()
Related articles
Assets Javadoc | Atlassian Support | Atlassian Documentation - as mentioned in the Atlassian article, you can review the Assets Javadoc pages by navigating to
https://docs.atlassian.com/insight/<version>
using the corresponding Assets version, for instance: https://docs.atlassian.com/assets/10.4.8/ObjectFacade class info (used in this KB article) https://docs.atlassian.com/assets/10.4.8/com/riadalabs/jira/plugins/insight/channel/external/api/facade/ObjectFacade.html