Expand my Community achievements bar.

When building a map in a loop, the first time through the map key is wrong

Avatar

Former Community Member
Hi,



I'm seeing some weird behaviour with the map data type. I'm trying to

convert a list of attachments to a map of attachments. I have a map with

subtype Document. I'm using a loop to iterate over the list and add

the attachments to the map. My problem is that the first time through

the loop, the map key is always being set the the value of the variable

used in the route condition (i.e. if I start the loop at 1, the map key

is set to an int value of 1, if I start the loop at 2, the map key is

set to 2). The second time through the loop, the proper key is being

used (which is a string value) and causes a java.lang.NumberFormatException.



If I add a value to the map before entering the loop, everything works ok.



Before entering the loop I have a variable mapping of:

/process_data/@count = count(/process_data/attList)

/process_data/@idx = 0



I have a conditional route of:

/process_data/@idx < /process_data/@count



And inside the loop, I have variable mappings:

/process_data/@idx = /process_data/@idx + 1

/process_data/@attName = getDocAttribute(/process_data/attList[number(/process_data/@idx)],"name")

/process_data/attMap[@id=/process_data/@attName] = /process_data/attList[number(/process_data/@idx)]



The first iteration always results in a map key equal to the value of the idx variable.



Not sure if I'm doing something wrong, or there's a bug.



Thanks.
2 Replies

Avatar

Level 10
I believe xPath is 1 based so start your loop at index 1 instead or 0.



Jasmin

Avatar

Former Community Member
Hi,

I do increment the index by 1 prior to using it.

What I've found is that if I first pull the document out of the list

and assign it to a Document variable, then insert it in the map

it works correctly.



For example....



/process_data/@idx = /process_data/@idx + 1

/process_data/@attName = getDocAttribute(/process_data/attList[number(/process_data/@idx)],"name")

/process_data/attDoc = /process_data/attList[number(/process_data/@idx)]

/process_data/attMap[@id=/process_data/@attName] = /process_data/@attDoc



I have since found it has nothing to do with the loop, as I get the

same behaviour outside the loop.