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

Insight groovy behaviour for custom field form value update

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

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.

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. 

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)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Send an Email or Publish to Confluence - What should you do with your release notes?

Background When you hear the words ‘Release notes’, almost always you think of an unsolicited email from a software vendor. But I am here to tell you that from our data, sending release notes via E...

130 views 1 2
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