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

How to store insight attributes?

Hello! Im trying to calculate insight attributes of the certain object, it returns value, but i can 't store ( change value in insight). Have any ideas how to solve this problem? :(

 

 

import com.atlassian.jira.component.ComponentAccessor;

Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"));

def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);
def objects = iqlFacade.findObjectsByIQLAndSchema(4, "Name IN (testObject)").id;

def totalLicense = objectFacade.loadObjectAttributeBean(1383, 554).getObjectAttributeValueBeans()[0].getValue() //100 int
def givenLicense = objectFacade.loadObjectAttributeBean(1383, 555).getObjectAttributeValueBeans()[0].getValue() //29 int
def freeLicense = objectFacade.loadObjectAttributeBean(1383, 556).getObjectAttributeValueBeans()[0].getValue() // 71

 

newValue = freeLicense - givenLicense;

def newTotalLicense = objectFacade.loadObjectAttributeBean(1383, 554).setObjectTypeAttributeId(newValue)
// it returns 58

try {

objectTypeAttributeBean = objectFacade.storeObjectAttributeBean(newTotalLicense)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}

Screenshot 2022-01-10 at 13.09.17.png

 

Screenshot 2022-01-12 at 13.52.13.png

1 answer

1 accepted

0 votes
Answer accepted

I see a couple of things wrong with your script.

1) loadObjectAttributeBean may return a null if that attribute is not populated. 

2) You need to create a ObjectAttributeValueBean to add to the ObjectAttributeBean, you can't just set it with setObjectTypeAttributeId

If you are 100% confident that attributes 554, 555, and 556 will always be populated, you can keep that section as is, otherwise, you should split the loadObjectAttributeBean and the getObjectAttributeValueBeans into separate lines and check if the first exists before accessing the second (and probably skip the rest of the script if it doesn't exist).

But as for building and storing the attribute value, something like this should work:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade

def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeFacade)
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)
def iqlFacade
= ComponentAccessor.getOSGiComponentInstanceOfType(IQLFacade)

def objects = iqlFacade.findObjectsByIQLAndSchema(4, "Name IN (testObject)").id;

//OTA (Object Type Atrribute) represents the deffinition of the attribute
def
totalLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(554)
def giveLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(555)
def freeLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(556)

//OA (Object Atrribute) represents an instance of an OTA for a specific object
def
totalLicenseOA = objectFacade.loadObjectAttributeBean(1383, totalLicenseOTA.id)
def givenLicenseOA = objectFacade.loadObjectAttributeBean(1383, giveLicenseOTA.id)
def freeLicenseOA = objectFacade.loadObjectAttributeBean(1383, freeLicenseOTA.id)

//OAV (Object Attribute Values contains all the values (1 to many) for the OA instance.
//You can return type-specific values with "getIntegerValue()" method for example or use the generic "getValue()" method
//Only retreive the value if the attribute exist
def
totalLicense = totalLicenseOA && totalLicenseOA.objectAttributeValueBeans[0].value //100 int
def givenLicense = givenLicenseOA && givenLicenseOA .objectAttributeValueBeans[0].value //29 int
def freeLicense = freeLicenseOA && freeLicenseOA .objectAttributeValueBeans[0].value //71 int

if(freeLicense && givenLicense ) {
def newValueTotalLicense = freeLicense - givenLicense
if(newValueTotalLicense != totalLicense) {
def newTotalLicenseOAB = object.createObjectAttributeBean(totalLicenseOA)
def newTotalLicenseValueBean = newTotalLicenseOAB.createObjectAttributeValueBean()
newTotalLicenseValueBean.setValue(newValueTotalLicense)
newTotalLicenseOAB.setObjectAttributeValueBeans([newTotalLicenseValueBean])
try {
def storedOAB = objectFacade.storeObjectAttributeBean(newTotalLicenseOAB)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

@Peter-Dave Sheehan 

Hello, thank you so much, i also input next bunch of code ( because 3 imports turns in red):

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")

 

But i still have an errors :(

 

Screenshot 2022-01-13 at 09.35.33.png

Looks like I made some cut/paste mistake somewhere along the way ... 

Here is a fresh copy of the script:

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

ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
ObjectTypeAttributeFacade objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)
IQLFacade iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(IQLFacade);

def objects = iqlFacade.findObjectsByIQLAndSchema(4, "Name IN (testObject)").id;
def totalLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(554)
def giveLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(555)
def freeLicenseOTA = objectTypeAttributeFacade.loadObjectTypeAttribute(556)

def totalLicenseOA = objectFacade.loadObjectAttributeBean(1383, totalLicenseOTA.id)
def givenLicenseOA = objectFacade.loadObjectAttributeBean(1383, giveLicenseOTA.id)
def freeLicenseOA = objectFacade.loadObjectAttributeBean(1383, freeLicenseOTA.id)

def totalLicense = totalLicenseOA && totalLicenseOA.objectAttributeValueBeans[0].value //100 int
def givenLicense = givenLicenseOA && givenLicenseOA.objectAttributeValueBeans[0].value //29 int
def freeLicense = freeLicenseOA && freeLicenseOA.objectAttributeValueBeans[0].value //71 int
givenLicenseOA.objectAttributeValueBeans*.integerValue
if(freeLicense && givenLicense ) {
def newValueTotalLicense = freeLicense - givenLicense
if(newValueTotalLicense != totalLicense) {
def newTotalLicenseOAB = object.createObjectAttributeBean(totalLicenseOA)
def newTotalLicenseValueBean = newTotalLicenseOAB.createObjectAttributeValueBean()
newTotalLicenseValueBean.setValue(newValueTotalLicense)
newTotalLicenseOAB.setObjectAttributeValueBeans([newTotalLicenseValueBean])
try {
def storedOAB = objectFacade.storeObjectAttributeBean(newTotalLicenseOAB)
} catch (Exception vie) {
log.warn("Could not update object attribute due to validation exception:" + vie.getMessage());
}
}
}

Given the way you were instantiating the classes I was assuming that you were using this script within the Insight Groovy Console, not the Script Runner Console.

In the script runner console, yes, you have to include the "WithPlugin" to get access to the classes. But in the Insight Console, those classes are inherently available (and the scriptrunner built-in classes just as WithPlugin and PluginModule cannot be used).

So the script as I pasted above should work in insight consoles (or in a groovy script called from Insight Automation).

Or to use the same script in scriptrunner, instead of using ComponentAccessor.getOSGiComponentInstanceOfType method, you can instead use this:

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
@PluginModule IQLFacade iqlFacade

@Peter-Dave Sheehan 

tried both scripts in automation (Insight) and Script console...nothing

 

Screenshot 2022-01-13 at 15.18.40.pngScreenshot 2022-01-13 at 15.23.09.png

On the second screenshot, delete lines 15-17 ... they are duplicated of 10-12.

But if you are going to use this in Insight Automation, you can't user scriptrunner to validatate. The first screenshot is expected to have errors. But the script will compile and execute correctly by insight.

Thank you! I ll check it, you helped so enought

answer accepted

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: Fine Software

Happy New Year, everybody! What better way to kick off 2022 than with a new Marketplace Partner Spotlight ✨ This month, we're getting to know Daniel Franz, co-founder of Fine Software. Fine Software ...

417 views 2 15
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