Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Groovy script doesn't update insight attribute

Edited

Hi,
we have a groovy script- postfunction which should get a value from a custom field (quantity) and subtract this value from available quantity (insight attribute) and add it to reserved quantity ( also insight attribute). But when I run it - nothing is updated, the test script result return "true". 
I've checked all the id present in the script - all are right.
Please help me to understand what is wrong with the script

Here is the script;

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 org.apache.log4j.Logger;
import org.apache.log4j.Level;


def AddIntegerToInsightAttribute (Issue issue, int jiraFieldId, int insightFieldId, int attributeID, Boolean addInt = true) {

def log = Logger.getLogger("com.onresolve.jira.groovy");

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

/* This is the custom field with the value you want to add to an object attribute */
CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(jiraFieldId); // Quantity Delivered (Integer) custom field

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(insightFieldId); // Stock Item (Insight Object single select) custom field
def insightObjects = issue.getCustomFieldValue(insightCustomField); // "issue" variable is always accessible in post function scripts.

/* This is the priority object type attribute and the one we want to modify */
def origValue = 0; // Default to an original value if attribute is empty

if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Load the attribute bean for reserved quantity */
def objAttrBean = objectFacade.loadObjectAttributeBean(insightObject.getId(), attributeID)
if (objAttrBean == null) {
/* If no value, throw error */
log.error("InsightAttributeAndCustomFieldManipulation:AddIntegerToInsightAttribute - Insight object update failed: object attribute has no value - Issue: " + issue + ", jiraFieldId: " + jiraFieldId + ", insightFieldId: " + insightFieldId + ", attributeID: " + attributeID + ", addInt = " + addInt);
return false;
} else {
/* If value was defined, store it */
origValue = objAttrBean.getObjectAttributeValueBeans().first().getIntegerValue();
}

/* Set the new values to the object attributes */
if (issue.getCustomFieldValue(jiraCustomField) == null ) {
log.info("InsightAttributeAndCustomFieldManipulation:AddIntegerToInsightAttribute - Integer parsing failed: custom field has no value - Issue: " + issue + ", jiraFieldId: " + jiraFieldId);
return false;
}
def jiraQuantity = issue.getCustomFieldValue(jiraCustomField).toInteger();

/* Clean the list of values */
def values = objAttrBean.getObjectAttributeValueBeans();
values.clear();

/* Create a new unsaved value */
def newObjAttrValBean = objAttrBean.createObjectAttributeValueBean();

if (addInt) {
newObjAttrValBean.setIntegerValue(origValue + jiraQuantity); // Set the Quantities in Insight Object
} else {
newObjAttrValBean.setIntegerValue(origValue - jiraQuantity); // Set the Quantities in Insight Object
}


/* Add the new value to the attribute */
values.add(newObjAttrValBean); // Add the value to the object attribute
objAttrBean.setObjectAttributeValueBeans(values);

/* Store the object attribute into Insight. */
try {
objAttrBean = objectFacade.storeObjectAttributeBean(objAttrBean);
} catch (Exception vie) {
log.error("InsightAttributeAndCustomFieldManipulation:AddIntegerToInsightAttribute - Insight object update failed: Could not update object attribute due to validation exception:" + vie.getMessage() + " - Issue: " + issue + ", jiraFieldId: " + jiraFieldId + ", insightFieldId: " + insightFieldId + ", insightAttributeName: "+ insightAttributeName + ", addInt = " + addInt);
}
}
}

/* Done! :) */
return true;
}


AddIntegerToInsightAttribute(issue, 14339, 14329, 5930, false);
AddIntegerToInsightAttribute(issue, 14339, 14329, 5931, true);
AddIntegerToInsightAttribute(issue, 14340, 14330, 5930, false);
AddIntegerToInsightAttribute(issue, 14340, 14330, 5931, true);
AddIntegerToInsightAttribute(issue, 14341, 14331, 5930, false);
AddIntegerToInsightAttribute(issue, 14341, 14331, 5931, true);
AddIntegerToInsightAttribute(issue, 14342, 14332, 5930, false);
AddIntegerToInsightAttribute(issue, 14342, 14332, 5931, true);
AddIntegerToInsightAttribute(issue, 14343, 14333, 5930, false);
AddIntegerToInsightAttribute(issue, 14343, 14333, 5931, true);


return true;

// 14339 Quantity (Integer) custom field
// 14329 Stock Item (Insight Object single select) custom field
// 5930 Available Quantity (Insight Object Attribute)
// 5931 Reserved Quantity (Insight Object Attribute)

 

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

👋 Looking for 15-20 volunteers to test Atlassian training content

Hi everyone! Are you interested in beta testing Atlassian University’s newest (unreleased!) training course? We’re looking for 15-20 volunteers to test our newest training course, Basic reporting...

831 views 25 28
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you