Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Extract Version Picker value without brackets []

Madhusudhan Matrubai
Contributor
May 29, 2018

I have a requirement to extract the value present in a custom field of type "Version Picker" .

If I do this

def releaseVerField = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_19641")
def releaseVerFieldValue = issue.getCustomFieldValue(releaseVerField).toString()

 

I get the output as [7.2] with square brackets. Can someone please advise how to get only the value and ignore the brackets.

1 answer

1 accepted

2 votes
Answer accepted
Nic Brough -Adaptavist-
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.
May 29, 2018

The [] are a string representation of the fact you've got an list of somethings back from the field, not a string.

Version pickers can hold many options, so your getCustomFieldValue is getting a list (often it'll be a list of one item), then just using "toString" outputs a converted value.

You should not convert it, but iterate over the values and use .getName() or whatever you need to pull out the data you're looking for.  If you can be absolutely sure it's only going to have one value, then just grab the first element from the list.

David
Contributor
May 21, 2019

Hey @Nic Brough -Adaptavist-, I'm looking to do just this. Can you give an example of it. If I use this:

issue.get("customfield_10002")*.name.flatten()

I get the value with brackets. There will always only be one value here, how can I iterate just the value using getName?

Nic Brough -Adaptavist-
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.
May 21, 2019

If it always has one value only, you can be lazy.  Issue.get("customfield_10002")[0] will fetch the first element of the array.

David
Contributor
May 28, 2019

@Nic Brough -Adaptavist- Thanks for getting back to me, so I gave Issue.get("customfield_10002")[0] a try and it returns this:

CustomerOrganizationImpl{id=159, name=some company name}

I'm trying to get the name value without brackets. It appears the first element contains both the id and name.

David Harkins
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.
June 9, 2021

Adding a reply to @David 

As I had the same issue and found a way to achieve it, though i was looking for the ID not the name.

change: value.id

to: value.name

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;

def value = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10110));

def OrgID = value.id[0]
log.info("OrgID:" + OrgID)

return OrgID

Suggest an answer

Log in or Sign up to answer