Referencing records and fields via RecordRef and FieldRef

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.

How to do it...

The RecordRef and FieldRef datatypes will be used to write a function comparing two records from the same table.

  1. Create a codeunit in the object designer and declare a local function CompareRecords in the codeunit. This is the function that will do the comparison. Set the function's return type to Boolean.
  2. Access the C/AL Locals in the function 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.
  3. Switch to the function's local variables and declare two variables:

    Name

    DataType

    FieldRef

    FieldRef

    I

    Integer

    The FieldRef variable should be an array of two elements, so change its Dimensions property the same way, as for the RecRef.

  4. The following function code will compare two records passed into it:
            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); 
    
  5. Trigger OnRun of the codeunit will invoke the function. Two local variables must be declared in the trigger:

    Name

    DataType

    Subtype

    Customer

    Record

    Customer

    RecRef

    RecordRef

    RecRef is the array that will be passed to the function CompareRecords, so in its properties set Dimensions = 2.

  6. In this example, we are comparing records from the Customer table. Records to be compared must be retrieved from the database first to get references. This code in the OnRun trigger will do that:
           Customer.GET('10000'), 
           RecRef[1].GETTABLE(Customer); 
           Customer.GET('20000'), 
           RecRef[2].GETTABLE(Customer); 
     
           MESSAGE(FORMAT(CompareRecords(RecRef))); 
    
  7. Save the codeunit as 50011Compare 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.
  8. Now open the Dynamics NAV role-tailored client, navigate to /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.
  9. In the 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.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.235.144