Assets - Groovy script - Set attribute value by performing math operations from 2 other attributes.

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 based on calculations/Math operations from 2 other attributes of the same object within Assets.

Other examples can be found in 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 Service Management 5.4.0

Groovy script has code to calculate the target attribute based on the source attributes. The attributes that are used in these calculation are of type "Integer". This is used in combination with Assets Automation as shown in image below.

(Auto-migrated image: description temporarily unavailable)
(Auto-migrated image: description temporarily unavailable)

Update Attribute based on Math operations from 2 other attribute values

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 import com.atlassian.jira.component.ComponentAccessor; 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 objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(1565).createMutable() //The id of final attribute to be updated def newObjectAttributeBean; int attributeRef1 = 1563 // Source attribute id. int attributeRef2 = 1564 // Source attribute id. def objectAttribute1 = objectFacade.loadObjectAttributeBean(object.getId(), attributeRef1) def objectAttribute2 = objectFacade.loadObjectAttributeBean(object.getId(), attributeRef2) if (objectAttribute1 && objectAttribute2) { /* Create the new attribute bean based on the value */ int x = objectAttribute1.getObjectAttributeValueBeans()[0].getValue() int y = objectAttribute2.getObjectAttributeValueBeans()[0].getValue() def z = x - y // We are calculating difference here. But change it according to requirement. newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(object, objectTypeAttributeBean, z.toString()); /* Load the attribute bean */ def objectAttributeBean = objectFacade.loadObjectAttributeBean(object.getId(), objectTypeAttributeBean.getId()); if (objectAttributeBean != null) { /* If attribute exist reuse the old id for the new attribute */ newObjectAttributeBean.setId(objectAttributeBean.getId()); } /* Store the object attribute into Insight. */ try { objectTypeAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean); } catch (Exception vie) { log.warn("Could not update object attribute due to validation exception:" + vie.getMessage()); } }
Updated on April 8, 2025

Still need help?

The Atlassian Community is here for you.