How to get only the key from groovy when running .getCustomFieldValue()

Ron Lambregts August 2, 2022

Hi All,

I've  a groovyscript where i want to find an objectID of a field from inside. It succeeds partially.

i use:

def app_newcustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14100");
def app_newValue = issue.getCustomFieldValue(app_newcustomField);

And as result i get :

app_newValue = [Interne Test apparaat (DM-453739)]

I only want the Key DM-453739 and not also the label. 

Can somebody help me with the right notation to get it?

Thx

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 2, 2022

Hi @Ron Lambregts 

 

I guess you are using a field that is not a standard type field.

 

First, do you expect 1 or multiple values ?

If only one you can get rig of the [ ] using first() fucntion.

def app_newcustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_14100");
def app_newValue = issue.getCustomFieldValue(app_newcustomField)?.first();

 

Then you can use a regex to get what is between the ().

 

Regex is (?<=\()(.*?)(?=\))

 

The code should be something like

app_newValue =  (app_newValue =~ / (?<=\()(.*?)(?=\))/)[ 0 ]​[ 1 ]

 

You may have to add some code to validate there is something that have match

 

Ron Lambregts September 9, 2022

HI @Florian Bonniec  it helped me a lot. Thx !

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.0.0
TAGS
AUG Leaders

Atlassian Community Events