Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Insight attribute ID in post-function by Name through customfield selection of object?

Natalie Al-Delemi September 13, 2019

Hi, 

Is it possible to get the attribute ID from Insight by Name? I've been stuck with a post-function for 4 days and getting no where. The script works if I specify the ID 

def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(328);

 

Since the ID is changing based on the users selection it is not possible to use the previous solution. I need a way of either using Name instead of ID or get the ID in a variable through the selected object. I will put my code here for reference and grateful if anyone know how to solve it.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import java.sql.Timestamp;
import java.text.DateFormat
import java.util.Date

/* 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(10802); //This is the Jira custom field called "Updated gate date"

/* This is the custom field where the object/s you want to set the value */
CustomField insightCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10208); // This is the Jira custom field containing the gate
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(328);

if (insightObjects != null) {
insightObjects.each{insightObject ->
/* Create the new attribute bean based on the value */
def newValue = issue.getCustomFieldValue(jiraCustomField);

my = DateFormat.getDateInstance().format(newValue);

def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(insightObject, objectTypeAttributeBean, my);
/* 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());
}
}
}

 

2 answers

1 accepted

0 votes
Answer accepted
Ilya Turov
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.
September 13, 2019

Hello, you can find insight object using IQLFacade.findObjectsByIQLAndSchema(schemaId, iql), then you can get the id from the object

also, I guess you are using scriptrunner, you can shorten declaring plugin's classes like this:

import com.onresolve.scriptrunner.runner.customisers.PluginModule 
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectAttributeBeanFactory objectAttributeBeanFactory

see: https://scriptrunner.adaptavist.com/latest/jira/scripting-other-plugins.html 

0 votes
Natalie Al-Delemi September 15, 2019

Hello Ilya, Thanks for the reply.

I'm new to the scripting world, so could you please show me how I can get the attribute ID using your recommendation?

In my code you will find the object being stored in insightObjects and the attribute ID for Date is 328 and this is the one I would like to read from the object.

Ilya Turov
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.
September 16, 2019

Oh, at first I thought you wanted to find insight object by it's name, not it's attribute.

If you have your insight object, you can get it's attribute by name like this:

import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade

insightObject.objectAttributeBeans.find {
  objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "attribute name"
}

at least, I do it like this

Like # people like this
Natalie Al-Delemi September 16, 2019

I tried to shorten plugin declaring like you suggested but I'm getting the following error:

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

GroovyInsightException: startup failed: Script1.groovy: 8: unable to resolve class com.onresolve.scriptrunner.runner.customisers.WithPlugin @ line 8, column 1. import com.onresolve.scriptrunner.runner.customisers.WithPlugin ^ Script1.groovy: 7: unable to resolve class com.onresolve.scriptrunner.runner.customisers.PluginModule @ line 7, column 1. import com.onresolve.scriptrunner.runner.customisers.PluginModule ^ 2 errors ')

 

This is the declaring code I'm currently using:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import java.sql.Timestamp;
import java.text.DateFormat
import java.util.Date
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectAttributeBeanFactory objectAttributeBeanFactory
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade

Natalie Al-Delemi September 16, 2019

I tried to shorten plugin declaring like you suggested but I'm getting the following error:

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

GroovyInsightException: startup failed: Script1.groovy: 8: unable to resolve class com.onresolve.scriptrunner.runner.customisers.WithPlugin @ line 8, column 1. import com.onresolve.scriptrunner.runner.customisers.WithPlugin ^ Script1.groovy: 7: unable to resolve class com.onresolve.scriptrunner.runner.customisers.PluginModule @ line 7, column 1. import com.onresolve.scriptrunner.runner.customisers.PluginModule ^ 2 errors ')

 

This is the declaring code I'm currently using:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import java.sql.Timestamp;
import java.text.DateFormat
import java.util.Date
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.factory.ObjectAttributeBeanFactory
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule ObjectAttributeBeanFactory objectAttributeBeanFactory
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade

Ilya Turov
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.
September 16, 2019

hmh, are you using scriptrunner or diferent plugin? 

because if it's the latter, you can't use scriptrunner's classes

Natalie Al-Delemi September 16, 2019

this is the script runner version I'm using: Version:5.5.6.1-jira8

If I can't use the classes with this version, could you please help me solve it with what I currently have? I have added the entire script of post-function in the begining of this post. It is just a matter of the somehow storing the attribute ID in an int variable and use it in (def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(328);) instead of the 328. since loadObjectTypeAttributeBean only takes int. 

 

Very grateful if you help me solve it. Been stuck for many days now :S

Natalie Al-Delemi September 16, 2019

To be specific about the errors I'm getting:

Based on your suggestions I tried to use the this to first find the object. (It seems that insightObjects just gets the value from jira custom field and not the object. Value contains value+ key): 

def objects = iqlFacade.findObjectsByIQLAndSchema(1, "\"object\" = \"" + insightObjects + "\""); // See the complete list of possible IQL on the Insight Query Language documentation page

def object = objects[0];

this returns empty since I can't find the relevant IQL matching the value. tried "\"object\" but doesn't seem to work

 

Later I'm using the following to get attribute ID based on previous value. But can't know if it works unless I get the first to work:

int attributeID = object.objectAttributeBeans.find {
objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "Date"}

Ilya Turov
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.
September 16, 2019

as far as I understand, you are getting your insight objects from a custom field, it's fine, but it always returns a list (even if you specify it's a single value):

def insightObjects = issue.getCustomFieldValue(insightCustomField)

so I guess we can work without IQL here (I brought it up because I misunderstood you at firts)

and find attribute id like this:

def obj = insightObjects[0]

obj.objectAttributeBeans.find {
objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "attribute name"
}?.objectTypeAttributeId
Like # people like this
Natalie Al-Delemi September 16, 2019

Thanks for your quick reply, Perhaps I'm confusing you but really appreciate your patience with me trying to understand the issue.

If I use what you suggested, what variable shall I replace the 328 value in after getting the ID? 

(def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(328);

 

According to your suggestion the following shall return the attribute ID for Date which in this case is 328. I need to use the returned value in the script above.

def obj = insightObjects[0]
obj.objectAttributeBeans.find {
objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "Date"
}?.objectTypeAttributeId

 

Many thanks

Natalie Al-Delemi September 16, 2019

ok now I get it. I actually replaced the 328 with:

obj.objectAttributeBeans.find {
objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "Date"
}?.objectTypeAttributeId

 

and now it works! Many thanks for the help

Like # people like this
Antoine Berry
Community Champion
September 16, 2019

See @Natalie Al-Delemi , this is the magic of the community ! :D

Natalie Al-Delemi September 16, 2019

Yes it is really wonderful. Thanks for the advice and grateful for the solution.

😁

Stefan Krooshof
Contributor
October 30, 2020

I had a similar issue, and this thread helped me solve it. 

In my use case I needed to update multiple attributes over multiple object types. The attributes have different attribute id's, but the same name

The scripts loops through list of objects found by IQL, finds attribute by name and replace with id for use with loadObjectTypeAttributeBean

objects.each {

def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectAttributeBeans.find {
objectTypeAttributeFacade.loadObjectTypeAttributeBean(it.objectTypeAttributeId).name == "Attribute Name"
}?.objectTypeAttributeId)
def newObjectAttributeBean = objectAttributeBeanFactory.createObjectAttributeBeanForObject(it, objectTypeAttributeBean, value)
def objectAttributeBean = objectFacade.loadObjectAttributeBean(it.getId(), objectTypeAttributeBean.getId())

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

Thank you @Natalie Al-Delemi and @Ilya Turov ! 

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events