Forums

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

Velocity accessing collections by index

Walter_Zambotti August 30, 2019

In the following code I am trying to access a collection with a single member.  The foreach works as described but I am unable to access items in the collection by index.

#set($customFields = $customFieldManager.getCustomFieldObjectsByName("Property"))

$customFields[0].name

#if(!$customFields.empty)
  #foreach($customField in $customFields)
     $customField.name 
  #end
#end

Prints

 [Property][0].name

Property

 

What am I not understanding?

2 answers

0 votes
Walter_Zambotti September 1, 2019

I got this to work by using the .get() method!

#set($customFields = $customFieldManager.getCustomFieldObjectsByName("Property"))

#set($propertyField = $customFields.get(0)) ## Can only be the "Property" field (or bust)

then to get the "property" field value I did this:

$issue.getCustomFieldValue($propertyField.id)

No more loops required!!!

Iago Docando
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 2, 2019

Glad to see you worked it out just about fine.

In my experience this sort of matters can get (no pun intended) a bit tricky ;)

0 votes
Iago Docando
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 30, 2019

I'm no programmer so maybe there's some error in your code that I can't see (I have very limited programming capabilities and I usually learn as I need). Aside from that possibility, I had problems in the past using $customFieldManager.getCustomFieldObjectsByName( )

Maybe you could try:

  • $!{cfUtils.getValue($issue, "customfield_10000")} - Retrieving string representation of customfield value or null
  • $!{cfUtils.getRenderedValue($issue, "customfield_10000")} - Retrieving rendered value of customfield or null
  • $!{cfUtils.getVal($issue, "customfield_10000")} - Retrieving actual value object of customfield value or null
Walter_Zambotti August 31, 2019

It does return the expected custom field and it does contain the expected value if I use :

$customField.value

Of course this works only in the loop!

If  use:

$customFields[0].value // I tried [1] as well

then that too doesn't work. 

So at the moment to access an item in an array I seem forced to use a loop. Which means iterating through the loop until I reach the required index.

Suggest an answer

Log in or Sign up to answer