You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.