Append Items to an Insight Object Attribute

Anna Protopapa February 14, 2024

We are trying to write a post function to append items to an Insight Object Attribute. 

We have used the sample code but it overwrites the existing attribute. Also only one value (the first one in the field) is set as attribute 


/* Get Insight Object Attribute Facade from plugin accessor */
Class objectTypeAttributeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade");
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeAttributeFacadeClass);


/* Get the factory that creates Insight Attributes */
Class objectAttributeBeanFactoryClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory");
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(objectAttributeBeanFactoryClass);

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(13613); // Employees to Give Access

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(13679); // Database (my) - Owner
def insightObjects = issue.getCustomFieldValue(insightCustomField); // "issue" variable is always accessible in post function scripts.

/* This is the Database object type attribute (Access) and the one we want to modify */
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(2528);


if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Create the new attribute bean based on the value */
def newValue = issue.getCustomFieldValue(jiraCustomField);
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, newValue[0].getName());

/* Load the attribute bean */
def objectAttributeBean = objectFacade.loadObjectAttributeBean(insightObject.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 {
objectAttributeBean = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

/* Done! :) */
return true;

1 answer

1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2024

Hi Anna

When the attribute already contains a value, you must first read that value then modify it (append the new user).

Here is a bit of a re-worked version of your script with that in mind:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory

/* Get Insight Object Attribute Facade from plugin accessor */
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade);
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade);

/* Get the factory that creates Insight Attributes */
def objectAttributeBeanFactory = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectAttributeBeanFactory);

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.customFieldManager.getCustomFieldObject(13613); // Employees to Give Access

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.customFieldManager.getCustomFieldObject(13679); // Database (my) - Owner
def insightObjects = issue.getCustomFieldValue(insightCustomField); // "issue" variable is always accessible in post function scripts.

/* This is the Database object type attribute (Access) and the one we want to modify */
def accessAttributeId = 2528
def accessOTABean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(accessAttributeId);


if (insightObjects != null) {
insightObjects.each { ObjectBean insightObject ->
//get the value from the issue. This is the source of the data to add
def newValue = issue.getCustomFieldValue(jiraCustomField)

//get the current value for the attribute on the object
def currentAccessOABean = insightObject.objectAttributeBeans.find { it.objectTypeAttributeId == accessOTABean.id }

def newCurrentAccessOABean //ObjectAttributeBean, either new or modified
if (currentAccessOABean) {
//if we have a value, we must modify it
newCurrentAccessOABean = currentAccessOABean.createMutable()
//create oavBean to hold the new value
def newCurrentAccessValue = newCurrentAccessOABean.createObjectAttributeValueBean()
//populate the bean with the value
newCurrentAccessValue.setValue(accessOTABean, newValue[0].key)
//add the new oavBean to the existing list
newCurrentAccessOABean.objectAttributeValueBeans.add(newCurrentAccessValue)

} else {
//if there is no value at the moment, we must create a new bean
/* Create the new attribute bean based on the value */
newCurrentAccessOABean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, accessOTABean, newValue[0].key);
}
/* Store the object attribute into Insight. */
try {
objectAttributeBean = objectFacade.storeObjectAttributeBean(newCurrentAccessOABean);
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

/* Done! :) */
return true;

Also, you'll note that I changed your newValue[0].getName() to getKey. Insight stores the key for jira users, not the username. Maybe in your environment, users have mostly had their key equal to their name, but that changed with Jira 8.7 (I think) and up.

Anna Protopapa March 14, 2024

Hi!

Thank you for your response.

The getKey is not working, we changed back to getName()

We found the following problems

1. It appends only one user to Insight attribute

2. Instead of user we prefer to use object attributes (employee object)

Do you have any suggestions for the above?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events