Groovy Insight basic function

Daniel Ben Eliyahu March 7, 2021

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

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 7, 2021

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)
Daniel Ben Eliyahu March 14, 2021

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.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2021

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

Daniel Ben Eliyahu March 15, 2021

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)
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 15, 2021

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
AUG Leaders

Atlassian Community Events