Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

get the value of an Elements connect field in Scriptrunner scripted field

Dan W
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.
July 31, 2020

I want to create a scripted field that has its value set based on the contents of a elements connect field.  So far I have been working in the scriptrunner console just trying to get the value of the field and it keeps returning null.  The Elements connect field is a snapshot field that reads an Active directory value.  I have verified the value is set on the field so it isn't null.  

Here is the code I have so far.

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 = "Issuekey-123458";
def customFieldId = "customfield_12633";

String displayValue = "";
Object displayResult = fieldDisplayService.getDisplayResult(issueKey, customFieldId);
if (displayResult != null) {
displayValue = displayResult.getDisplay();
}
return displayValue;

 Anybody have an idea how I can get this to work.  The long term goal of this field will be to compare the approvers title to a list of titles we use to stop the approval process.  We are currently using a regular expression in several automation's to compare the titles and I want to consolidate where we are doing this process to one location so when a persons title change we don't have to update multiple automation condition checks.

3 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Nicolas Esteves August 5, 2020

Hi @Dan W

I'm Nicolas from Elements, vendor of Elements Connect.

If you get a "null" result with this piece of code, that means there are 3 possible explanations:
- "customfield_12633" is not a valid reference
- "Issuekey-123458" is not a valid issue key
- field is empty in the given issue

Here is an example with a Scripted Field:

2020-08-05 10_25_16-Window.png

Can you please try again by checking all these points?

If you still have issues, please open a support ticket from this portal: https://support.elements-apps.com/portal

Thank you.

Kind regards,
Nicolas.

Dan W
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.
August 6, 2020

I changed the issue key and the custom field id in the script that I posted, but I verified that I have valid information.  

I displayed the field on the edit form and it has a value

I created the scripted field using your example and changing to match my environment and it still returns null.  Also tried using the read field key method same result.  I will open a ticket with your support desk.  

 

Thanks

Victor Cheung January 12, 2024

hi Nicolas, how can we retrieve the saved value of an Elements connect custom field without triggering the configured REST API call with the field?

We just want the saved value, we don't need the saved value to be resolved to it's "label" value.

We are currently using:

issue.getCustomFieldValue(fieldElementsConnect)

But this will trigger it to call the REST API (as if we are in the UI and trying to populate the field--but in this case it's not UI situation and the field already has the saved key value in it, and we just want to retrieve this key value--we do not need it to trigger REST API call to resolve the key at all)

Thanks!

victor

Nicolas Esteves January 12, 2024

Hi @Victor Cheung,

You could use this script to get the raw value saved in an Elements Connect custom field (we call this value the "Key", and what you call label is called "Display value" in our documentation):

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)

Please replace issueKey and customFieldId by appropriate values.

Source: Elements Connect documentation

Does that make sense to you or do you need anything else?

Have a nice day.

Kind regards,
Nicolas.

Like Victor Cheung likes this
Victor Cheung January 12, 2024

Hi Nicolas,

Thank you so much for your quick help on this!  It's perfect and exactly what I was hoping for!  (We were using the IFieldDisplayService and didn't know about the IFieldValueService)

Thank You!!

Best regards,

victor

Like Nicolas Esteves likes this
Nicolas Esteves January 13, 2024

You're welcome @Victor Cheung !

If you have any other question about Elements Connect or any other Elements apps, feel free to contact us directly through our Support portal: https://support.elements-apps.com/ 

Have a nice day.

Kind regards,

0 votes
Muhammad Ramzan_Atlassian Certified Master_
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.
September 23, 2020

I am also getting null by using your code, everything is correct issue id , field id.

 

I am surprised for one field its returning value

For one drop down field its returning all values not the selected one

 

for all other fields its returning null

0 votes
Daniel Hornig
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 5, 2020

Hi,

with your function from the docs you will get the display value. Have you configured your display view? You can try it in the Field Configuration.

Have you tried to get the key value instead of the display value?

Under Read Field Key:

https://doc.elements-apps.com/elements-connect/latest/api-guide/java-api/scriptrunner-use-elements-connect-api-in-a-script

Daniel Hornig
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 23, 2020

For everyone who wants to get current value on a Screen like in a transition where you just changed the value and want to react on this change with a Behaviour then you must use a more complicated way:

String value = (String) getFieldById(customfieldId).getFormValue();
if(value != null && value != ""){
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(value)
assert object instanceof Map
assert object.inputValues instanceof List
if(object.inputValues){
return object.inputValues.toList();
} else {
return [""];
}
} else {
return [""];
}
 
Like Nicolas Esteves likes this
Nicolas Esteves April 13, 2021

Hi,

Indeed, you can find more information about Behaviours and Elements Connect here: https://doc.elements-apps.com/elements-connect/latest/faq/can-elements-connect-fields-be-manipulated-from-a-scriptrunner-behaviours-script

Kind regards,

Daniel Hornig
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 13, 2021

Hi,

I forgot its now in the docs of Elemenst Connect but only for the new version (6.x). For the update from version 5.x to 6.x i created a function to secure all scripts are functional before and after.

String[] elements_connect(def s){
String e = s as String;
if(e != null && e.length() > 0){
if(e.startsWith("{")){
def jsonSlurper = new JsonSlurper();
def json = jsonSlurper.parseText(e);
String[] connectFieldKeys = (String[]) json["inputValues"];
return connectFieldKeys;
} else if(e.startsWith("[")){
String[] connectFieldKeys = s as String[];
return connectFieldKeys;
} else {
return null;
}
} else {
return null;
}
}
Like Nicolas Esteves likes this
TAGS
AUG Leaders

Atlassian Community Events