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
4,359,862
Community Members
 
Community Events
168
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("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

0 votes
Answer accepted

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

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)
}
}
}

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

Suggest an answer

Log in or Sign up to answer
TAGS
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