With the availability of Insight with all JSM Service Desk subscriptions, I'm seeing an uptick in community questions about ScriptRunner and Insight Custom fields and Insight objects.
As a long-time ScriptRrunner user, when we purchased Insight last year for our new JSM implementation, I started exploring their API and figuring out how to leverage scriptrunner for many different automation needs we had.
The Insight API and data model is quite complex and attempting to build a simple rule or condition based on the value of an attribute in a selected object in an Insight custom field can require many many lines of code.
Over the months, I've developed a fairly wide range of functions that make interractions with insight a breeze.
Here is an example...
Say you want to show/hide some fields using Behaviour base on an attribute of object selected in a custom field...
Behaviour will give you a string representation of the ObjectKey for the selected object.
So, you will need to
It would look something like this:
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
@WithPlugin('com.riadalabs.jira.plugins.insight')
@PluginModule ObjectFacade objectFacade
@PluginModule ObjectTypeFacade objectTypeFacade
@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
def attributeName = 'My Attribute Name'
def insightFormField = getFieldById(fieldChanged)
String objectKey = insightFormField.value //this assumes this insight custom field doesn't allow multiple
def object = objectFacade.loadObjectBean(objectKey)
def objectType = objectTypeFacade.loadObjectType(object.objectTypeId)
def attributeBean = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id).find{ it.name == attributeName}
def attributeValue = object.objectAttributeBeans.find{it.objectTypeAttributeId == attributeBean.id}?.objectAttributeValueBeans*.value
And you haven't even started to write the behaviour logic yet ... all that just to get a simple attribute value. And only if that attribute is a simple type like text.
What if the attribute I'm interested in is not directly in the selected object but in one of its outbound reference?
For example, imagine a User object where the Manager attribute points to another object and I'm interested in looking up the title of the manager for the selected User.
What if I told you that this is all I have to write in my behaviour scripts to get the manager's title?
import com.qad.common.jira.utils.InsightUtils
def userFormField = getFieldById(fieldChanged)
def managerTitle = InsightUtils.getAttributeValueFromDotNotation(userFormField.value, 'Manager.Title')
Much simpler yeah?
What about having some lookup in a script (either behaviour or postfunction) in the Customer portal? You might have come across an issue where the portal user doesn't have access to view the insight objects. You might want to be able to perform some script-level lookup as a different user.
Well, that is all possible using this utility class that I've developed and I am now sharing with the community:
https://bitbucket.org/peter_dave_sheehan/groovy/src/master/jiraserver/insightUtils/
Obviously, since this is provided for free, there is no guarantee this will work for you in all your use cases or that I will help with support.
But if you get into troubles, tag me in a community post and I might be able to help.
Have fun scripting!
Peter-Dave Sheehan
Atlassian Administrator/Groovy Developer
QAD
Santa Barbara, California
526 accepted answers
29 comments