Insight groovy extract attributes value from an Object

Priska Aprilia April 12, 2019

Hi,

I need to write a Groovy script in workflow validator when an issue is submitted via a Customer through Jira Service Desk Customer Portal.

We would like to check whether the reporter's company has a valid contract with us or not. If not, the reporter will not be able to submit the ticket.

We store the Company information as Insight Object, this Company object has one-to-many relationship with the Contact object where the reporter information resides. The primary key for the Contact object will be the email address.


We are able to retrieve the Contact object based on the reporter's email. However, we didn't find any way to extract the attribute value from the object, i.e the Company object.

Please see the following Groovy script.

* get Reporter email address */
def reporterEmail = issue.getReporter().getEmailAddress();
/* Custom field for the organisation */

/* The object facade class */
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");

/* The reference to the object facade which you can use in your code */
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

/* Load objects with a specific value for a specified attribute */
def objectTypeAttributeId = 30; // The object type attribute id
def objects = objectFacade.findObjectBeansByAttributeValue(objectTypeAttributeId, "=", reporterEmail); // Operators can be "=", "!=", "like", "<", "<=", ">", ">="
/* If this is a mandatory field you should be able to do this: */
log.info("Insight object: " + objects[0]);
if(objects.size() > 0){
def contactObject = objects[0];
//how to get the attribute value from object above.
//this project has attributes of name, email, phone and object reference to other Insight object
// log.info(attributes);
}
return objects

How can we get the attributes from the contactObject in the script above?

2 answers

1 accepted

2 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2019

Hi @Priska Aprilia ,

To retrieve the attribute value, you can use (of course adapt to your attribute type)

Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
def objectAttribute = null
def attributeRef = 51

if(contactObject){
objectAttribute = objectFacade.loadObjectAttributeBean(contactObject.getId(), attributeRef)
}
if(objectAttribute){
def objectAttributeValue = objectAttribute.getObjectAttributeValueBeans()
return objectAttributeValue[0]
}
return null

attributeRef can be either the id or the attribute name (but of course I would recommend using the id).

You can check the javadoc.

Antoine 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 16, 2019

Hi @Priska Aprilia , 

Did that help ?

Antoine

Priska Aprilia April 1, 2020

Hi @Antoine Berry 

Sorry for this super late reply. Yes, it helped. I just tried the code that you provided.
Thanks for the help!

Like Antoine Berry likes this
sunilkumar Shangishetty December 16, 2020

Hi Priska,

I'm new to JIRA, i'm trying to do similar kind of automation to update company field on Issue with Insight Object attribute when the reporter email is same as one of insight object attribute(email).

 

I have used your code, but i'm getting this errors.

ERROR - groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.getReporter() is applicable for argument types: () values: []
ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

 Can you please suggest me what to do.

Any kind of help is appreciated.

Thank you,

Steve Letch January 7, 2022

Hey @Antoine Berry thanks, will give it a go, I need to query an object in a behaviors rule to decide whether or not a field is hidden or visible + mandatory. Will see if I can work with your example., thanks

0 votes
Stephen Letch October 15, 2020

hi @Antoine Berry 

 

Any chance I could get some help with something similar, I need a Behavior to make a text field mandatory if an attribute of an Insight object is x

 

In my case it's make Justification mandatory if 'Change Freeze Status' = Running

and preferably hide the hide the field on the specific transition screen entirely if 'Change Freeze Status' = Stopped

but it's not necessary

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 23, 2020

Hi @Stephen Letch ,

I do not use Insight anymore, so I can only advise you to try standard behaviour scripts to retrieve the value of a custom field and display your results in the logs.

Also, there are some inconsistencies between ScriptRunner behaviours and Insight, see this issue. But I am not sure if this is blocking in your case.

sunilkumar Shangishetty December 16, 2020

Hi Antonie,

I'm new to Jira and i have a requirement to set a issue feild with Insight object attribute when reporters email same as email in insight.

Can you please help me with the code/syntax how to start the code in script listener.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events