Example Groovy script to update Object Type attribute value
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
The example Groovy script in this documentation allows to update object attributes with non-Object type attribute types, for example, Text type. In order to update an attribute of type Object type, the object key has to be used.
Solution
Here's an example code snippet to reference an object with key "ITSM-2" to a Host object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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(159).createMutable() //The id of the attribute
/* Create the new attribute bean based on the value */
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(object, objectTypeAttributeBean, "ITSM-2");
/* 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());
}
Was this helpful?