ScriptRunner - Use Elements Connect API in a script


Table of contents


Use case

You need to manipulate Connect field in a ScriptRunner script:

  • Read field key (stored value)
  • Set field key
  • Get field display value

because of the particular storage format of Elements Connect fields, you shall use the Elements Connect API to perform these operations.



ScriptRunner Behaviours case

This page explains how the stored value of an Elements Connect field shall be manipulated from a server side script (such as a Post function validator)

If you want to manipulate the value of an Elements Connect field from a Behaviours script, please refer to this page.



Script examples:

Get field display value

The following example shows how to get the display result of a Elements Connect custom field (customfield_10101) on issue TEST-1:

This script uses the FieldDisplayService, check out the Javadoc to learn more about this service.


Get Elements Connect field display value
import com.atlassian.jira.component.ComponentAccessor
 
def pluginAccessor = ComponentAccessor.getPluginAccessor();
def plugin = pluginAccessor.getPlugin("com.valiantys.jira.plugins.SQLFeed");
def serviceClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.IFieldDisplayService");

def fieldDisplayService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass);
 
def issueKey = "ISSUE-12345";
def customFieldId = "customfield_12345";
 
String displayValue = "";
Object displayResult = fieldDisplayService.getDisplayResult(issueKey, customFieldId);
if (displayResult != null) {
	displayValue = displayResult.getDisplay();
}
return displayValue;


Get field display value from a specific view

It's also possible to get the display result from a specific view by mentioning this view in the getDisplayResult method. As a reminder, a Connect field can have different display views, as explained here: Display view configuration

The following example shows how to get the display result of the Dashboard Gadget view 

To know which views can be used in this method, check out the Javadoc to learn more about this Service


import com.atlassian.jira.component.ComponentAccessor
 
def pluginAccessor = ComponentAccessor.getPluginAccessor();
def plugin = pluginAccessor.getPlugin("com.valiantys.jira.plugins.SQLFeed");
def serviceClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.IFieldDisplayService");
def viewClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.View")

def fieldDisplayService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass);
 
def issueKey = "ISSUE-12345";
def customFieldId = "customfield_12345";
 
String displayValue = "";
Object displayResult = fieldDisplayService.getDisplayResult(issueKey, customFieldId, viewClass.DASHBOARD);
if (displayResult != null) {
	displayValue = displayResult.getDisplay();
}
return displayValue;


Write field key

Use this script to programmatically set the key of an Elements Connect field.

(info) It is not possible to set the display value of an Elements Connect field

This script uses the FieldValueService, check out the Javadoc to learn more about this service.


Set Elements Connect field value
import com.atlassian.jira.component.ComponentAccessor
 
def pluginAccessor = ComponentAccessor.getPluginAccessor();
def plugin = pluginAccessor.getPlugin("com.valiantys.jira.plugins.SQLFeed");
def serviceClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.IFieldValueService");
 
def fieldValueService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass);
 
def issueKey = "ISSUE-12345";
def customFieldId = "customfield_12345";
def connectKeyValue = "10001";
 
fieldValueService.setFieldValue(issueKey, customFieldId, connectKeyValue);

(info) If the Connect field is a multi value field, the Keys should be passed as an array:

def connectKeyValue = ["10001","10002"];


Read field key

Use this script to programmatically get the key of an Elements Connect field.

This script uses the FieldValueService, check out the Javadoc to learn more about this service.


Set Elements Connect field value
import com.atlassian.jira.component.ComponentAccessor
 
def pluginAccessor = ComponentAccessor.getPluginAccessor();
def plugin = pluginAccessor.getPlugin("com.valiantys.jira.plugins.SQLFeed");
def serviceClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.IFieldValueService");
 
def fieldValueService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass);
 
def issueKey = "ISSUE-12345";
def customFieldId = "customfield_12345";

return fieldValueService.getFieldValues(issueKey, customFieldId);


Compute fields value

Use this method if you want to programmatically compute fields values.

This is commonly used on Read only fields when a parent field is updated by a script.

This is similar to the "compute Connect field" post function.

This script uses the FieldValueService, check out the Javadoc to learn more about this service.


Set Elements Connect field value
import com.atlassian.jira.component.ComponentAccessor
 
def pluginAccessor = ComponentAccessor.getPluginAccessor();
def plugin = pluginAccessor.getPlugin("com.valiantys.jira.plugins.SQLFeed");
def serviceClass = plugin.getClassLoader().loadClass("com.valiantys.nfeed.api.IFieldValueService");
 
def fieldValueService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass);
 
def issueKey = "ISSUE-12345";
def customFieldId1 = "customfield_10001";
def customFieldId2 = "customfield_10002";

return fieldValueService.computeFieldsValue(issueKey, [customFieldId1, customFieldId2]);



"Cannot find matching method" error message

When using the Elements Connect API in a script, some error messages are displayed:







These are false positives and you don't have to worry about them. These errors are generated by the Scriptrunner compiler because it doesn't know the Elements Connect classes currently in use. Since it doesn't know them, it doesn't know the methods used and considers that they don't exist.

So, you should ignore these error messages because it doesn't prevent the script from running correctly.