Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

a Scriptrunner Behavior with a issue picker field and a Insight referenced custom field

Hi, I have a Scriptrunner Behavior who (on the service desk portal) has to take over the values ​​of an issue selected in an issue picker field in Insight Custom Fields. Only the value of an Insight referenced custom field is not taken over. 

This is the script. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
def issueManager = ComponentAccessor.getIssueManager() as IssueManager
def issuePickerCustomFieldValue = getFieldById(getFieldChanged()).value
def relatedIssue = issueManager.getIssueByCurrentKey(issuePickerCustomFieldValue as String)

def EngineerPACustomField = getFieldByName("Engineer E/PA")
def EngineerPACustomFieldObject = customFieldManager.getCustomFieldObjectsByName("Engineer E/PA")[0]
def EngineerPACustomFieldValue = (EngineerPACustomFieldObject ? relatedIssue.getCustomFieldValue(EngineerPACustomFieldObject) : null) as String
if (EngineerPACustomFieldValue) {
def objectKeyStr = EngineerPACustomFieldValue.substring(EngineerPACustomFieldValue.indexOf("(")+1,EngineerPACustomFieldValue.indexOf(")"))
def SystemIntegratorCustomField = getFieldByName("System integrator")
def SystemIntegratorCustomFieldObject = customFieldManager.getCustomFieldObjectsByName("System integrator")[0]
def SystemIntegratorCustomFieldValue = (SystemIntegratorCustomFieldObject ? relatedIssue.getCustomFieldValue(SystemIntegratorCustomFieldObject) : null) as String
if (SystemIntegratorCustomFieldValue) {
def objectKeyStr1 = SystemIntegratorCustomFieldValue.substring(SystemIntegratorCustomFieldValue.indexOf("(")+1, SystemIntegratorCustomFieldValue.indexOf(")"))
def MedewSystemIntegratorCustomField = getFieldByName("Medewerker System integrator")
def MedewSystemIntegratorCustomFieldObject = customFieldManager.getCustomFieldObjectsByName("Medewerker System integrator")[0]
def MedewSystemIntegratorCustomFieldValue = (MedewSystemIntegratorCustomFieldObject ? relatedIssue.getCustomFieldValue(MedewSystemIntegratorCustomFieldObject) : null) as String
if (MedewSystemIntegratorCustomFieldValue) {
def objectKeyStr2 = MedewSystemIntegratorCustomFieldValue.substring(MedewSystemIntegratorCustomFieldValue.indexOf("(")+1, MedewSystemIntegratorCustomFieldValue.indexOf(")"))

 "PA Project" is a Issue picker field.

"Medewerker System integrator" is an Insight referenced custom field and this is not copied from the chosen issue.

"Engineer E / PA" (User Picker (single user) field) and "System Integrator" (Insight Object/s field) are copied correctly.

How do I get this working?

regards, Marco


2 answers

1 accepted

0 votes
Answer accepted


I solved it by make for "Medewerker System integrator" a "normal" Insight Custom Field instead of a Insight referenced custom field.

Regards, Marco

0 votes

I'm not sure why your script is not working... but I have a few tips for you.

1) accessing ObjectFacade and other insight classes is much easier in scriptrunner than in insight groovy console. Try like this:

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@PluginModule ObjectFacade objectFacade

3) Behavior FormField obtained by name can return the field ID. That makes findings customFieldObject much easier:

def EngineerPACustomFieldObject = customFieldManager.getCustomFieldObject(EngineerPACustomField.fieldId)

2) Insight custom fields are returning Arrays of Insight ObjectBean (regardless if it allows 1 or many values, except when they are empty). So you don't need to parse the string, just get the object directly

def EngineerPACustomFieldValue = (EngineerPACustomFieldObject ? relatedIssue.getCustomFieldValue(EngineerPACustomFieldObject) : []) as ArrayList<ObjectBean>
if (EngineerPACustomFieldValue) {
def objectKey = EngineerPACustomFieldValue[0].objectKey

3) Insight fields in behaviours return a string when a  single value is preset and an array of string where there are multiple values. But always require an array of strings when setting the form values (even for just a single value).

So putting it all together and avoiding code repeats ... you should be able to use this:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@PluginModule ObjectFacade objectFacade
def issueManager = ComponentAccessor.issueManager
def issuePickerCustomFieldValue = getFieldById(fieldChanged).value
def relatedIssue = issueManager.getIssueObject(issuePickerCustomFieldValue as String)

def fieldNamesToMap = ['Engineer E/PA','System integrator','Medewerker System integrator']
def formField = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObject(formField.fieldId)
def values = (relatedIssue.getCustomFieldValue(customField) ?: []) as ArrayList<ObjectBean>;
def objectKeys = values*.objectKey

@Peter-Dave Sheehan  Thank you for the tips.   Greetings, Marco

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

172 views 1 6
Read article

Atlassian Community Events