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.

Logan Gold avatar image Logan Gold ♦♦ commented ·

Hi @Borja Lorenzo, was Felix Möhlmann's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

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.

Borja Lorenzo avatar image Borja Lorenzo commented ·

I think I don’t quite understand it completely. Do you have any example of the application of this function in any tutorial, example, etc.? In my case, I detect the row through the if condition

0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann Borja Lorenzo commented ·

Take this section of a made up bill of material. It contains size and weight of some SKUs.

1727358368683.png

If you wanted to get, for example, the weight of "GTZ_A3X" you would use

Table().getValueByKey("GTZ_A3X", 5, 1)

to get the value from the from the fifth column in a row where the first column is eqaul to "GTZ_A3X".

You use it when the row number is not known. In your code you iterate through the entire table, meaning you know which row you are looking at. So you should use "Table()[row][col]" to read values from that given row.

1 Like 1 ·
1727358368683.png (8.0 KiB)