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 this operations.
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.
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;
Write field key
Use this script to programmatically set the key of an Elements Connect field.
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.
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);
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.
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.
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]);