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

Groovy Insight basic function

Hello, I have a working code that extract Insight attribute data by using attribute name:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
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.channel.external.api.facade.ObjectTypeAttributeFacade
import org.apache.log4j.Logger
import org.apache.log4j.Level
import java.text.SimpleDateFormat
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

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

def f = getFieldById(getFieldChanged())
String objectKey = f.getValue()
def insightObject = objectFacade.loadObjectBean(objectKey)
int objectId = insightObject.getId()

String InsightFirstName = "First Name"

def InsightFNAttribute = objectFacade.loadObjectAttributeBean(objectId, InsightFirstName)
def InsightFNAttributeValues = InsightFNAttribute.getObjectAttributeValueBeans()
def InsightFNAttributeValue = InsightFNAttributeValues[0]
def FirstName = InsightFNAttributeValue.value

String InsightLastName = "Last Name"

def InsightLNAttribute = objectFacade.loadObjectAttributeBean(objectId, InsightLastName)
def InsightLNAttributeValues = InsightLNAttribute.getObjectAttributeValueBeans()
def InsightLNAttributeValue = InsightLNAttributeValues[0]
def LastName = InsightLNAttributeValue.value

 

___________________________________________________________________________

FirstName & Lastname successfully get the correct data.

How can I turn this piece of code into a working function?
I have a lot more attribute to extract and I dont want to duplicate those lines again.
I want to call a function that will extract that data for me with just 1 line of code per attribute.

 

Thank you in advance,

Daniel.

1 answer

Hi @Daniel Ben Eliyahu , I think it is enough to implement function similar to:

def getAttributeValue(def objectFacade, def objectId, def attributeName){
def attributeBean = objectFacade.loadObjectAttributeBean(objectId, attributeName)
def attributeValues = attributeBean.getObjectAttributeValueBeans()
def attributeValue = attributeValues[0]
def attribute = attributeValue.value

return attribute
}

and call it like:

def FirstName = getAttributeValue(objectFacade, objectId, InsightFirstName)
def LastName = getAttributeValue(objectFacade, objectId, InsightLastName)

Hello Martin,

 

Thank you for your answer.

Why am I getting an error like this?

 

[Static type checking] - The variable [objectFacade] is undeclared.

@ line 38, column 25.

 

and line 38 is:

def attributeBean = objectFacade.loadObjectAttributeBean(objectId, attributeName)

 

Thanks!

Daniel.

Hi @Daniel Ben Eliyahu ... sorry, a little mistake :) I adjusted my answer...

Thank you @Martin Bayer _MoroSystems_ s_r_o__ !

I've applied your code and I get another error on this line:

 def attributeBean = objectFacade.loadObjectAttributeBean(objectId, attributeName)

error.jpg

Is it something Wrong I did?

 

my current code is: (on ScriptRunner Behaviours)

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
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.channel.external.api.facade.ObjectTypeAttributeFacade
import org.apache.log4j.Logger
import org.apache.log4j.Level
import java.text.SimpleDateFormat

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


def f = getFieldById(getFieldChanged())
//f.setHelpText("RawValues: ${f.rawValue} <br><font color=green>Value: <b>$f.value</b></font> <br>FromValue: $f.formValue ")
String objectKey = f.getValue()
def insightObject = objectFacade.loadObjectBean(objectKey)
int objectId = insightObject.getId()

def OnlyNumbers = extractInt(objectKey)

def getAttributeValue(def objectFacade, def objectId, def attributeName){

def attributeBean = objectFacade.loadObjectAttributeBean(objectId, attributeName)
def attributeValues = attributeBean.getObjectAttributeValueBeans()
def attributeValue = attributeValues[0]
def attribute = attributeValue.value

return attribute
}

def FirstName = getAttributeValue(objectFacade, objectId, InsightFirstName)

Hi @Daniel Ben Eliyahu , static code check is not so important in groovy  because we are not using "types" as we use in Java.

This code should work. We need to only fix runtime errors, but you have to execute the code so we know if there are any errors.

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

59 views 0 5
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