Insight groovy behaviour for custom field form value update

Sebastian Wrzesinski
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 27, 2021

Hello Community,

I'm trying to create a Behaviour Script for some Creation Screen, which anytime field Asset (Insight customfield) will be changed, will change the value of other field called Unit (also an Insight customfield).

Unit is one of the Attributes of an Asset in Insight database and what I would like to achieve is that Unit field will have the same option chosen as this Attribute value for selected Asset. 

What I have for the moment and it obviously doesn't work:

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade;

ObjectFacade objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade);

def unit = getFieldByName("Unit")
def asset = getFieldById(getFieldChanged())
//getting Asset field value
def assetValue = asset[0]
//getting Unit Attribute value of selected Asset
def assetAttrValue = objectFacade.loadObjectAttributeBean(assetValue.getId(), 3566).getObjectAttributeValueBeans()[0];
def assetOrgUnitValue = assetAttrValue.getValue()
//setting Unit form value to one taken from Asset
unit.setFormValue(assetOrgUnitValue)

 I tried many other options, but it looks like none was good enough :)

Can anyone help?

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 28, 2021

First, a tip...

You can get the ObjectFacade with

import com.onresolve.scriptrunner.runner.customisers.PluginModule
@PluginModule ObjectFacade objectFacade

Instead of using the getOsGiComponentINstanceOfType

Second:

getFieldById gets you a formField object. You need to fetch the value for it. For formField based on Insight Custom field, the value will be either an ArrayList of object Keys or a String objectKey. You get an array or not depending on whether multiple values have been selected or no (not based on whether multiples are allowed or not in the configs).

So if I know the field only allows 1, then I would get the key like this:

def assetFormField = getFieldById(fieldChanged)
def assetKey = assetFormField.value as String

If my field allows mulitple, I standardize the value return type like this 

def multiFormField= getFieldById(fieldChanged)
def objectKeys = (multiFormField.value instanceof List) ? multiFormField.value as List : [multiFormField.value as String]

Third:

Since the formField values are just strings of object keys... you'll need to get an ObjectBean before you can get the attribute and apply it (as it's object key) to the other field

def assetObj = objectFacade.loadObjectBean(assetKey)
def assetAttrBeans =
objectFacade.loadObjectAttributeBean(assetObj.id, 3566).objectAttributeValueBeans
if(assetAttrBeans){
unit.setFormValue(assetAttrBeans.first().value)
}

 

Put all together:

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.riadalabs.jira.plugins.insight")

@PluginModule ObjectFacade objectFacade

def unitFormField = getFieldByName("Unit")
def assetFormField = getFieldById(fieldChanged)

def assetKey = assetFormField.value as String
def assetObj = objectFacade.loadObjectBean(assetKey)
def assetAttrBeans =
objectFacade.loadObjectAttributeBean(assetObj.id, 3566).objectAttributeValueBeans
if(assetAttrBeans){
unitFormField.setFormValue(assetAttrBeans.first().value)
}

 I added some protection against the object not having an org unit attribute in which get [0] or .first() would give you an error.

Sebastian Wrzesinski
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 30, 2021

Hello Peter,

Many thanks for explanation, that's what I needed. Looks like FormFIelds were not clear to me :) 

Your code had a little issues:

1) I needed to keep:

import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade;

2) Value of assetAttrBeans.first() does not return object key, I needed to use different method:

 unitField.setFormValue("CMDB-" + assetAttrBeans.first().getReferencedObjectBeanId() as String)

Finally it works as expected. 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 30, 2021

Sorry about those mistakes. I wrote everything pretty much directly in the comment without testing.

I would recommend against forming the key yourself like you did.

Instead, I would do it like this (or something like it):

def assetObject = objectFacade.loadObjectBean(assetAttrBeans.first().referenceObjectBeanId)
unitField.setFormValue(assetObject.objectKey)
Like Michael Agius Muscat likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events