Assets - Update attribute value from a referenced object
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. 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
Solution
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
Update Attribute Value from a referenced Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 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())
}
Was this helpful?