question

Borja Lorenzo avatar image
0 Likes"
Borja Lorenzo asked Logan Gold commented

How to use getValuebyKey()

Hello,


I need help understanding the use of getValuebyKey(). In this custom code, I iterate through each of the rows to extract the content of the second column and then process that string. This table is imported from a database, so it comes with a default ID column. If the ID starts at 1 and increases progressively, the function works for me. But this table can depend on certain SQL filters that change that ID.


As I have it set up, Table("BD_CPLAB").getValueByKey(row, 4) or Table("BD_CPLAB").getValueByKey(row, 2), as seen in the following capture. Row is an int that takes its value from the total number of rows in the BD_CPLAB table, to iterate through all the rows.

1727352080327.png

In the Systems Console, I get several errors referring to the same thing because I have this function in other Custom Code of my PF. The curious thing is that it points to row 36, where there is no error as I have checked.


To solve it, I changed the expression to Table("BD_CPLAB")[row] or Table("BD_CPLAB")[row]. What I intended with the getValuebyKey() function is that with the first argument of the function, it would go to the row index (1, 2, or x) and point to the column I indicate…

1727352137596.pngbut what I have found is that it looks for a value equal to row in one of the rows?


I have two questions:


Can I use the getValuebyKey() function with this approach? How could I call the row index where the if condition is met?

Why is it pointing to an incorrect row in the System Console?

FlexSim 24.1.1
tablegetvaluebykeyid rowsearch row and column
1727352080327.png (243.8 KiB)
1727352137596.png (24.2 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Felix Möhlmann avatar image
1 Like"
Felix Möhlmann answered Felix Möhlmann commented

"getValueByKey(key, valueCol, keyCol)" is used to look for the first row where the value in the column keyCol is equal to key and return the value of column valueCol from that row.

https://docs.flexsim.com/en/24.2/Reference/CodingInFlexSim/FlexScriptAPIReference/Data/Table.html#Method-getValueByKey

If you know the row number, just use Table()[row][col].

The error message telling you the code line where the error happened is a new feature and doesn't always work, yet.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.