Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy code to remove value from an Insight object type attribute

Mariano Redondo January 7, 2022

I have a Jira service management custom field that stores an Insight object. This object type has in Insight an attribute of type "Object", and I need to remove its value when the issue transitions to an specific status.

There is an Insight postfunctions that allows to do this, but due to bug JSDSERVER-8437 it does not work when there is only one object stored on the attribute (which is my case). So I´m trying to write a groovy code to do this, but I´m stock receiving this error message as result:

 

class com.riadalabs.jira.plugins.insight.common.exception.GroovyInsightException

GroovyInsightException: ValidationInsightException: Validation errors were found: rlabs-insight-attribute-93: ErrorMessage{i18nKey='rlabs.insight.i18n.constraint.violation.ObjectAttributeValueBean.MinOne.value', parameters=[], additionalMessage=null}; '

The constraint violation confuses me as the attribute I´m trying to clean has 0 to 1 cardinality (in fact I can manually remove its value perfectly). Here the code I´m using, I´ll appreciate if you can help me find what I am doing wrong.

 

int insightSchemaID = 1;
int jiraCustomFieldIDForAffectedObject = 13813; // Jira custom field (Insight type) that stores the Insight object
int insightAtributoID = 93; // The Insight object attribute Id which value needs to be removed


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import java.util.ArrayList;
import java.util.stream.Collectors;


// Get Insight Object Facade from plugin accessor
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

// Get Insight Object Type Facade from plugin accessor
Class objectTypeFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade");
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectTypeFacadeClass);

// 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);


/* ------------------------------------------------------------- */
// 1. Get Jira Insight custom field value
/* ------------------------------------------------------------- */
CustomField disposalField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraCustomFieldIDForAffectedObject);

if (disposalField == null) {
return false;
}

def disposalID = disposalField.getValue(issue);
if (disposalID.isEmpty()) {
return false;
}


/* ------------------------------------------------------------------- */
// 2. Get the Insight object
/* ------------------------------------------------------------------- */

def obj = objectFacade.loadObjectBean(disposalID[0].id);


/* ------------------------------------------------------------------- */
// 3. Get the object attribute which value has to be removed
/* ------------------------------------------------------------------- */

def objAttrib = objectFacade.loadObjectAttributeBean(obj.getId(), insightAtributoID);

 

/* ------------------------------------------------------------------- */
// 4. Create the object type attribute to store
/* ------------------------------------------------------------------- */

def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(insightAtributoID);
def newObjectAttributeBean = obj.createObjectAttributeBean(objectTypeAttributeBean);

 

/* ------------------------------------------------------------------- */
// 5. Assign empty value to the atribute
/* ------------------------------------------------------------------- */

newObjectAttributeBean.setObjectAttributeValueBeans([]);

 

/* ------------------------------------------------------------------- */
// 6. Assign the attribute id to the object attribute
/* ------------------------------------------------------------------- */

// assign the id of the attribute (got in step 3)
newObjectAttributeBean.setId(objAttrib.getId());

 

/* ------------------------------------------------------------------- */
// 7. Object update
/* ------------------------------------------------------------------- */

objAttribute = objectFacade.storeObjectAttributeBean(newObjectAttributeBean);

 

 

 

1 answer

1 accepted

5 votes
Answer accepted
Mariano Redondo January 10, 2022

Found the solution, no need to make an update, attribute value can be deleted directly through objectFacade.deleteObjectAttributeBean(objAttrib.getId())

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events