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

Insight groovy extract attributes value from an Object

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

0 votes
Answer accepted
Antoine Berry Community Leader Apr 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 Apr 16, 2019

Hi @Priska Aprilia , 

Did that help ?

Antoine

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

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,

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 Oct 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.

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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

318 views 3 14
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