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

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles 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

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.


Update Attribute based on Math operations from 2 other attribute values
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());
    }

}

Last modified on May 7, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.