Referencing dynamic tables and fields
On occasions, we may need to retrieve data from the system, but not know in advance where that data should come from. NAV accommodates this by allowing you to reference tables and fields dynamically.
How to do it...
Let's start by creating a new codeunit from Object Designer.
Add a global function,
GetFirstRecord
:The function should take the following parameter:
Name
Type
TableNo
Integer
Now add the following local variables:
Name
Type
RecRef
RecordRef
FieldRef
FieldRef
With the cursor on the
FieldRef
variable, navigate to View | Properties or press Shift + F4.Let's set the following property:
Property
Value
Dimensions
2
Write the following code in the
GetFirstRecord
function:RecRef.OPEN(TableNo); IF RecRef.FINDFIRST THEN BEGIN IF RecRef.FIELDEXIST(1) THEN FieldRef[1] := RecRef.FIELDINDEX(1); IF RecRef.FIELDEXIST(2) THEN FieldRef[2] := RecRef.FIELDINDEX(2); IF FieldRef[1].ACTIVE AND FieldRef[2].ACTIVE THEN ...