The NAV datatype Record
provides an interface to tables in the database. A certain limitation of the Record type is that a declaration of a Record
variable is fixed in design time. Each variable must be linked to a table in the object design, and the link cannot be changed dynamically.
An alternative to static linking is the RecordRef
datatype. A variable of RecordRef
type is assigned to a specific table at runtime, but dynamics linking leads to higher complexity of code.
The RecordRef
and FieldRef
datatypes will be used to write a function comparing two records from the same table.
CompareRecords
in the codeunit. This is the function that will do the comparison. Set the function's return type to Boolean
.CompareRecords
, declare a parameter RecRef
of type RecordRef
. After that, open the parameter properties and set Dimensions = 2 to turn the parameter into an array with two elements. Close the parameters window.
Name |
DataType |
|
|
|
|
The FieldRef variable should be an array of two elements, so change its Dimensions property the same way, as for the RecRef
.
FOR I := 2 TO RecRef[1].FIELDCOUNT DO BEGIN FieldRef[1] := RecRef[1].FIELDINDEX(I); FieldRef[2] := RecRef[2].FIELDINDEX(I); IF UPPERCASE(FORMAT(FieldRef[1].CLASS)) = 'NORMAL' THEN IF FieldRef[1].VALUE <> FieldRef[2].VALUE THEN EXIT(FALSE); END; EXIT(TRUE);
OnRun
of the codeunit will invoke the function. Two local variables must be declared in the trigger:
Name |
DataType |
Subtype |
|
|
|
|
|
RecRef
is the array that will be passed to the function CompareRecords
, so in its properties set Dimensions = 2.
OnRun
trigger will do that:Customer.GET('10000'), RecRef[1].GETTABLE(Customer); Customer.GET('20000'), RecRef[2].GETTABLE(Customer); MESSAGE(FORMAT(CompareRecords(RecRef)));
50011
Compare Records and close the code editor. Run the codeunit from the object designer and review the result. A message box will pop up informing you that customer records are not equal./Departments/Financial Management/Receivables/Customers
and create a customer record. By default, it is assigned the number C00010
. Do not modify it. Create a second customer record and set Invoice Disc. Code = C00010. Now we have two customer records, identical in all fields except the primary key value.OnRun
trigger of the codeunit 50011
, change the customer IDs from 10000
and 20000
to C00010
and C00020
respectively. Run the codeunit again. Now the function will return a positive result, confirming that customers are identical.Code execution starts from the invocation of the OnRun
trigger when the codeunit is run. Here, the record reference is initialized and positioned on a database record. The most straightforward way to do so is to get the value from a Record
variable. Although this method imposes limitations on the code flexibility, in many cases it is justified by the simplicity of the code.
After assigning record references, trigger code calls the CompareRecords
function with the array of RecordRef
in parameters.
The invoked function will iterated on the list of record fields in a FOR
loop, starting from the field number 2. The first field is skipped, since this is the primary key value and cannot be equal in two different records. Number of loop iteration is defined by the function FIELDCOUNT
that returns the number of fields in a table.
To access each particular field inside the loop we use the FIELDINDEX
function. The function returns the reference to a field identified by its sequential number.
Before comparing two values, we ensure that the field has the Normal
class and skip FlowFields
and FlowFilters
, comparing only fields actually stored in the database.
The FieldRef.VALUE
function returns the actual value of the record field that can be compared or assigned to another variable.
3.142.235.144