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

Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 24, 2021

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("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
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(")"))
EngineerPACustomField.setFormValue(objectKeyStr)
}
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(")"))
SystemIntegratorCustomField.setFormValue(objectKeyStr1)
}
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(")"))
MedewSystemIntegratorCustomField.setFormValue(objectKeyStr2)
}

 "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

image.png

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 26, 2021

Hi,

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
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.
March 24, 2021

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
@WithPlugin('com.riadalabs.jira.plugins.insight')
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@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
@WithPlugin('com.riadalabs.jira.plugins.insight')
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean

@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']
fieldNamesToMap.each{fieldName->
def formField = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObject(formField.fieldId)
if(customField){
def values = (relatedIssue.getCustomFieldValue(customField) ?: []) as ArrayList<ObjectBean>;
if(values){
def objectKeys = values*.objectKey
formField.setFormValue(objectKeys)
}
}
}
Marco Brundel
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 26, 2021

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

TAGS
AUG Leaders

Atlassian Community Events