Get Insight attribute with Scriptrunner Behavior

Jan Scherks November 25, 2020

Hi,

I'm trying to get value from Insight Object Attribute (High, Medium, Low) and use that value in IF statement.

Following code is working in Script console:

def customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObject("customfield_27522")
def customFieldValue = issue.getCustomFieldValue(customField)
def myObjectBean = customFieldValue[0]

log.info objectFacade.loadObjectAttributeBean(myObjectBean.getId(), 217).getObjectAttributeValueBeans()[0].getValue()

 But when adapt it to Behavior it's just not working at all:

def affectedComponents = getFieldById("customfield_27522") //Affected Component
def af_val = affectedComponents.getValue()
def affCompObjectBean = af_val[0].getId()
def affCompoValue = objectFacade.loadObjectAttributeBean(affCompObjectBean, 217).getObjectAttributeValueBeans()[0].getValue()

I'm getting Static type checking errors in both. But Console runs fine and gets me attribute value.

Did anyone manage to get Insight atributes working in Behaviors?

Thanks

Jan

1 answer

1 vote
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.
November 26, 2020

Hi @Jan Scherks I suggest you to start using loggin mechanism.

Static type checking errors does not mean the behaviour script is not executed successfully.

But when you are working with field "on the screen" I guess that for example for 

getFieldById("customfield_27522").getValue()

you won't get the same type of the value as in first script, so I would really log everything first:

def affectedComponents = getFieldById("customfield_27522") //Affected Component
log.error "Affected components:" + affectedComponents

def af_val = affectedComponents.getValue()
log.error "Affected components value:" + af_val

def affCompObjectBean = af_val[0].getId()
log.error "Aff bean:" + affCompObjectBean

def affCompoValue = objectFacade.loadObjectAttributeBean(affCompObjectBean, 217).getObjectAttributeValueBeans()[0].getValue()
log.error "Aff component bean value:" + affCompoValue

Then check your server log file (atlassian-jira.log) and post the output here and we can proceed with this information... 

Jan Scherks November 26, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

here's my log:

 

020-11-26 11:50:17,795+0000 http-nio-8080-exec-19 url:/rest/scriptrun.../runvalidator.json username:jscherks ERROR jscherks 710x146919x1 1i911eq 81.109.22.22,172.35.2.115 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] *************************************************************************************
2020-11-26 11:50:17,797+0000 http-nio-8080-exec-19 url:/rest/scriptrun.../runvalidator.json username:jscherks ERROR jscherks 710x146919x1 1i911eq 81.109.22.22,172.35.2.115 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: (create issue) project/issuetype: MRTP/Release, user: jscherks, fieldId: customfield_27525, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: java.lang.String.getId() is applicable for argument types: () values: []
Possible solutions: getAt(groovy.lang.IntRange), getAt(java.lang.String), getAt(int), getAt(groovy.lang.IntRange), getAt(groovy.lang.Range), getAt(groovy.lang.EmptyRange)
 at 970a9376984666c6ec6e8f5d8273db59.run(970a9376984666c6ec6e8f5d8273db59.groovy:30)
 at com.onresolve.jira.behaviours.BehaviourManagerImpl$_runValidator_closure2.doCall(BehaviourManagerImpl.groovy:194)
 at com.onresolve.jira.behaviours.BehaviourManagerImpl.runValidator(BehaviourManagerImpl.groovy:141)
 at com.onresolve.jira.behaviours.BehaviourManagerImpl.runValidator(BehaviourManagerImpl.groovy:91)
 at com.onresolve.jira.behaviours.restservice.BehavioursEndpoint$_runValidator_closure3.doCall(BehavioursEndpoint.groovy:137)
 at com.onresolve.jira.behaviours.restservice.BehavioursEndpoint$_runValidator_closure3.doCall(BehavioursEndpoint.groovy)
 at com.sun.proxy.$Proxy4316.get(Unknown Source)
 at com.onresolve.jira.behaviours.restservice.BehavioursEndpoint.withServletThreadLocal(BehavioursEndpoint.groovy:222)
 at com.onresolve.jira.behaviours.restservice.BehavioursEndpoint.runValidator(BehavioursEndpoint.groovy:131)
 ... 19 filtered
 at com.atlassian.plugins.rest.module.RestDelegatingServletFilter$JerseyOsgiServletContainer.doFilter(RestDelegatingServletFilter.java:159)
 ... 1 filtered
 at com.atlassian.plugins.rest.module.RestDelegatingServletFilter.doFilter(RestDelegatingServletFilter.java:69)
 ... 32 filtered
 at com.atlassian.servicedesk.internal.web.ExternalCustomerLockoutFilter.doFilter(ExternalCustomerLockoutFilter.java:55)
 ... 8 filtered
 at com.atlassian.jira.plugin.mobile.web.filter.MobileAppRequestFilter.doFilter(MobileAppRequestFilter.java:59)
 ... 4 filtered
 at com.atlassian.jira.plugin.mobile.login.MobileLoginSuccessFilter.doFilter(MobileLoginSuccessFilter.java:54)
 ... 3 filtered
 at com.atlassian.diagnostics.internal.platform.monitor.http.HttpRequestMonitoringFilter.doFilter(HttpRequestMonitoringFilter.java:55)
 ... 8 filtered
 at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
 ... 46 filtered
 at com.atlassian.ratelimiting.internal.filter.RateLimitFilter.doFilter(RateLimitFilter.java:73)
 ... 3 filtered
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.runWithChangedThreadName(AbstractThreadFilter.java:69)
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.doFilter(AbstractThreadFilter.java:53)
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.doFilter(AbstractThreadFilter.java:37)
 ... 14 filtered
 at com.atlassian.jira.security.JiraSecurityFilter.lambda$doFilter$0(JiraSecurityFilter.java:66)
 ... 1 filtered
 at com.atlassian.jira.security.JiraSecurityFilter.doFilter(JiraSecurityFilter.java:64)
 ... 16 filtered
 at com.atlassian.plugins.rest.module.servlet.RestSeraphFilter.doFilter(RestSeraphFilter.java:37)
 ... 19 filtered
 at com.atlassian.jira.servermetrics.CorrelationIdPopulatorFilter.doFilter(CorrelationIdPopulatorFilter.java:30)
 ... 5 filtered
 at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.lambda$invokeFilterChain$0(CustomerContextSettingFilter.java:212)
 at com.atlassian.servicedesk.internal.api.util.context.ReentrantThreadLocalBasedCodeContext.rteInvoke(ReentrantThreadLocalBasedCodeContext.java:136)
 at com.atlassian.servicedesk.internal.api.util.context.ReentrantThreadLocalBasedCodeContext.runOutOfContext(ReentrantThreadLocalBasedCodeContext.java:89)
 at com.atlassian.servicedesk.internal.utils.context.CustomerContextServiceImpl.runOutOfCustomerContext(CustomerContextServiceImpl.java:47)
 at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.outOfCustomerContext(CustomerContextSettingFilter.java:203)
 at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.doFilterImpl(CustomerContextSettingFilter.java:134)
 at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.doFilter(CustomerContextSettingFilter.java:123)
 ... 9 filtered
 at com.atlassian.jwt.internal.servlet.JwtAuthFilter.doFilter(JwtAuthFilter.java:37)
 ... 3 filtered
 at com.atlassian.ratelimiting.internal.filter.RateLimitPreAuthFilter.doFilter(RateLimitPreAuthFilter.java:71)
 ... 3 filtered
 at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
 ... 4 filtered
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.runWithChangedThreadName(AbstractThreadFilter.java:69)
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.doFilter(AbstractThreadFilter.java:53)
 at com.atlassian.ams.shipit.servlet.filter.AbstractThreadFilter.doFilter(AbstractThreadFilter.java:37)
 ... 3 filtered
 at com.atlassian.web.servlet.plugin.LocationCleanerFilter.doFilter(LocationCleanerFilter.java:36)
 ... 26 filtered
 at com.atlassian.jira.servermetrics.MetricsCollectorFilter.doFilter(MetricsCollectorFilter.java:25)
 ... 24 filtered
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
2020-11-26 11:50:17,799+0000 http-nio-8080-exec-19 url:/rest/scriptrun.../runvalidator.json username:jscherks DEBUG jscherks 710x146919x1 1i911eq 81.109.22.22,172.35.2.115 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Returning map: [:]

 Regards

Jan

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.
November 26, 2020

hi @Jan Scherks I think I will need the complete configuration of behaviours and screen maybe... it is weird that not even the first log record is present in the log file.

Jan Scherks November 26, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

Here's the Behavior config:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")


def subst = getFieldById("customfield_26720") //Substantial Change Area/Case
def affectedComponents = getFieldById("customfield_27522") //Affected Component

def messageText = """
<div>
<span class="aui-icon aui-icon-small aui-iconfont-error aui-icon-notification"></span>
Selected Affected Component has high criticality for Spain. You will have to answer below, an additional question to identify whether it is a Substantial Change or not.
</div>
"""
/* Get Insight Object Facade from plugin accessor */
ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
ObjectTypeAttributeFacade objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)
def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(217)

def field = getFieldById(getFieldChanged())
def marketsVal = field.getValue()
def af_val = affectedComponents.getValue()

//def affCompObjectBean = af_val[0].getId()
//def affCompoValue = objectFacade.loadObjectAttributeBean(affCompObjectBean, 217).getObjectAttributeValueBeans()[0].getValue()

if ((marketsVal==~/.*RC-94.*/ || marketsVal==~/.*RC-92.*/) && (af_val ==~ /RC-1/ || af_val =~ /RC-5/ || af_val =~ /RC-10/ || af_val =~ /RC-12/ || af_val =~ /RC-15/ || af_val =~ /RC-16/ || af_val =~ /RC-17/ || af_val =~ /RC-19/ || af_val =~ /RC-20/ || af_val =~ /RC-22/ || af_val =~ /RC-26/|| af_val =~ /RC-186/ )){
//if ((marketsVal==~/.*RC-94.*/ || marketsVal==~/.*RC-92.*/) && affCompoValue == "High"){

subst.setHidden(false)
subst.setRequired(true)
field.setHelpText(messageText)
} else {
subst.setHidden(true)
subst.setRequired(false)
subst.setFormValue(["",""])
field.setHelpText("")
}

Out of many I have following fields:

Affected components (Insight field) - customfield_27522

Market (Insight field) - where the Server side script is - field changed

When the script is just checking Insight object IDs (RC-26/RC-22/...) from the field value it's working but that approach is not suitable for large number of components with High Criticality.

So Market has to be either Spain (RC-94) or All Markets (RC-92) and one of the High Critical components (RC-26/RC-22/...)

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.
November 26, 2020

hi @Jan Scherks  thank you for the detailed information. First I would simplify script to debug it, try to use this one and post the log file here again :)

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")


def subst = getFieldById("customfield_26720") //Substantial Change Area/Case
log.error "Subst:" + subst

def affectedComponents = getFieldById("customfield_27522") //Affected Component
log.error "Affected components:" + affectedComponents

/* Get Insight Object Facade from plugin accessor */
ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
log.error "object facade:" + objectFacade

ObjectTypeAttributeFacade objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)
log.error "object type attribute facade:" + objectTypeAttributeFacade

def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(217)
log.error "object type attribute bean:" + objectTypeAttributeBean

def field = getFieldById(getFieldChanged())
log.error "field:" + field

def marketsVal = field.getValue()
log.error "marketsVal:" + marketsVal

def af_val = affectedComponents.getValue()
log.error "Affected components value:" + af_val

def affCompObjectBean = af_val[0].getId()
log.error "Affected component bean:" + affCompObjectBean

//def affCompoValue = objectFacade.loadObjectAttributeBean(affCompObjectBean, 217).getObjectAttributeValueBeans()[0].getValue()
Jan Scherks November 27, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__ ,

thanks for the script.

Unfortunately it's failing on the following line:

def affCompObjectBean = af_val[0].getId()

same error as with my script:

groovy.lang.MissingMethodException: No signature of method: java.lang.String.getId() is applicable for argument types: () values: []
Possible solutions: getAt(groovy.lang.IntRange), getAt(java.lang.String), getAt(int), getAt(groovy.lang.IntRange), getAt(groovy.lang.Range), getAt(groovy.lang.EmptyRange)

Anyway I was playing with different way how to check the Criticality and found the way this morning :)

import com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.customisers.PluginModuleimport com.onresolve.scriptrunner.runner.customisers.WithPluginimport com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacadeimport com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacadeimport com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.riadalabs.jira.plugins.insight")
@PluginModule IQLFacade iqlFacade

def subst = getFieldById("customfield_26720") //Substantial Change Area/Case
def affectedComponents = getFieldById("customfield_27522") //Affected Component

def messageText = """<div>    <span class="aui-icon aui-icon-small aui-iconfont-error aui-icon-notification"></span>    Selected Affected Component has high criticality for Spain. You will have to answer below, an additional question to identify whether it is a Substantial Change or not.</div>"""

def field = getFieldById(getFieldChanged())
def marketsVal = field.getValue()
def af_val = affectedComponents.getValue()
def value = 0
def afString = af_val.toString()

def highCriticality = iqlFacade.findObjectsByIQLAndSchema(1, "\"Spain Criticality\" IN (\"High\")")

if (afString.contains(",")) {
    highCriticality.each { object ->
    af_val.each { cf ->
    def string = object.toString()
     if (string.contains(cf.toString())) {value = 1}       
    }
   
}} else {
highCriticality.each { object ->
    def string = object.toString()
        if (string.contains(af_val.toString())) {value = 1}       
    }}

if (!af_val) {value = 0}
if ((marketsVal==~/.*RC-94.*/ || marketsVal==~/.*RC-92.*/) && value == 1 {
        subst.setHidden(false)
    subst.setRequired(true)
    field.setHelpText(messageText)   
} else {
    subst.setHidden(true)
    subst.setRequired(false)
    subst.setFormValue(["",""])
    field.setHelpText("")
}

Basically I'm searching for objects with IQL = "Spain Criticality" IN "High". Then iterate with value/s from Affected components field through the IQL results and storing the result into a variable. Because the value from Affected components field can have multiple value I have to check if it's single/multiple and iterate accordingly.

It will need a bit more polishing before releasing into Prod JIRA :)

Thank again for the help (Diky za pomoc ;) )

Regards

Jan

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.
November 28, 2020

OK @Jan Scherks so I guess it is working now... instead of "value" variable which is Integer, I suggest you to use boolean variable and true/false... it would be nicer solution :)

No problem ( neni zac :))

Martin

Suggest an answer

Log in or Sign up to answer