Assets - Update attribute value from a referenced object
Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
This article provides a groovy script example to update an attribute value from a referenced object within Assets.
Other examples can be be found on Groovy script examples. The groovy script could be executed in an Automation rule
Example
The following information is provided as-is. Atlassian Support cannot provide further assistance with the Groovy script described below
Script was tested against Jira 8.20 and Insight 9.1.3
// Classes imports
import com.atlassian.jira.component.ComponentAccessor
// Insight variables
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"))
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade"))
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory"))
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade"))
// Attributes definitions
// New attribute to be populated
def assetAttributeName = "New_Network"
// Object Type
def ownerAttributeName = "Network Interface"
// Related objects and their object type id's
def assetObject = object
//Change 'DN' to your Object Schema Key
def iql = "objectType=\"Scanning Information\" AND object HAVING inR(Key = DN-"+assetObject.getId()+")"
// Change '8' to your Object Schema ID
def ownerObject = iqlFacade.findObjectsByIQLAndSchema(8, iql)[0]
int assetObjectTypeId = assetObject.getObjectTypeId()
int ownerObjectTypeId = ownerObject.getObjectTypeId()
// Owner object type attribute bean (Network Interface of the Object Type)
def ownerObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(ownerObjectTypeId, ownerAttributeName)
// Owner object attribute bean (Network Interface of the owner object)
def ownerObjectAttributeBean = objectFacade.loadObjectAttributeBean(ownerObject.getId(), ownerObjectTypeAttributeBean.getId())
// Getting the value of the owner attribute (Network Interface value)
def ownerObjectAttributeValue = ownerObjectAttributeBean.getObjectAttributeValueBeans()[0].getValue()
// Asset object type attribute bean (Network Interface of the Object Type)
def assetObjectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(assetObjectTypeId, assetAttributeName)
// Creating a new value for the attribute in the Asset object (New Network Interface value of the Asset)
// Change 'DN' to your Object Schema Key
def newAssetObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(assetObject, assetObjectTypeAttributeBean, "DN-"+ownerObjectAttributeValue)
// Store the object attribute into Insight
try {
assetObjectTypeAttributeBean = objectFacade.storeObjectAttributeBean(newAssetObjectAttributeBean)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage())
}