How to use getValuebyKey()

How to use getValuebyKey()

borja_lorenzo4278
Enthusiast Enthusiast
98 Views
4 Replies
Message 1 of 5

How to use getValuebyKey()

borja_lorenzo4278
Enthusiast
Enthusiast

[ FlexSim 24.1.1 ]

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?

0 Likes
Accepted solutions (1)
99 Views
4 Replies
Replies (4)
Message 2 of 5

moehlmann_fe
Participant
Participant
Accepted solution

"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#Me...

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.

Message 3 of 5

borja_lorenzo4278
Enthusiast
Enthusiast

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
Message 4 of 5

moehlmann_fe
Participant
Participant

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.

Message 5 of 5

logan_gold
Community Manager
Community Manager

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