How to subtraction operation Groovy script in insight Post Functions

kahn chang May 23, 2018

Hi

I have CustomField:
CustomField 10328 is a standard number field
CustomField 10321 Mapping to insight objectType 100

objectTypeAttribute 1004 of insight objectType 100, type=defailt ,type value=integer

I need Update object attribute 1004 with the value “attribute 1004” Minus “CustomField 10328”

I have no experience. I tried to modify Groovy script examples, but I haven't touched on subtraction yet. I made an error.

 

*********************************************************************************

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;

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


/* 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(10328); // Change 10328 to the correct value

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10321); // Change 10321 to the correct value
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 objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(1004);

if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Create the new attribute bean based on the value */
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, issue.getCustomFieldValue(jiraCustomField));
/* 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;

*********************************************************************************

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

GroovyInsightException: No signature of method: com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory.createObjectAttributeBeanForObject() is applicable for argument types: (com.riadalabs.jira.plugins.insight.services.model.ObjectBean, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, java.lang.Double) values: [CGO52_ESFP-GE-SX-MM1310_H3C_TEST (NW-737281), ObjectTypeAttributeBean [id=1004, name=StockValue, type=DEFAULT], ...] Possible solutions: createObjectAttributeBeanForObject(com.riadalabs.jira.plugins.insight.services.model.ObjectBean, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, [Ljava.lang.String;), createObjectAttributeBeanForObject(com.riadalabs.jira.plugins.insight.services.model.ObjectBean, com.riadalabs.jira.plugins.insight.services.model.ObjectTypeAttributeBean, java.text.DateFormat, java.text.DateFormat, [Ljava.lang.String;)'

 

 

Please help me!

Best Regards

1 answer

0 votes
Ola_Melin
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 29, 2018

Hi, 

 

The createObjectAttributeBeanForObject takes a variable argument of strings as the latest parameter which might not be that intuitive. 

The trace indicates that you got a Double value. 

If you sent the last parameter to the createObjectAttributeBeanForObject as the following

String.valueOf(Math.round(issue.getCustomFieldValue(jiraCustomField)))

It should do the trick and of course you can switch the inner statement for the result of your arithmetic operation. 

Check out if it solves your use case. 

 

Cheers 

kahn chang July 5, 2018

hi

Thank you for your answer, but it's too complicated for me. I submitted a new function, because I believe it will be very useful.

https://jira.riada.se/browse/ICS-806

Cheers 

Aaron Whigham
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 12, 2023

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events