Access xRec-Data via RecRef?

ThalonThalon Member Posts: 10
Hello!

I wonder if it is somehow possible to access the data from a xRec by RecordRef?

To explain the background:
I'm working on an Export to an external system and I have to check if relevant fields have changed.
As I don't want to add code to each OnValidate-Trigger or do 50 "if Description <> xRec.Description" I'm looking for a more elegant solution, like accessing the fields via RecRef.

But I do not find a possibility to achieve this.

I'm open to suggestions (or a "not possible").

Thanks!
Thalon

Answers

  • AddoAddo Member Posts: 1
    Hi Thalon,

    There is no built-in xRecRef in NAV, but you could try to use GETTABLE function together with xRec:
    RecRef.GETTABLE(Rec);
        xRecRef.GETTABLE(xRec);  //xRecRef here is just a simple RecRef variable
    
        FieldRef := RecRef.FIELD(YOUR_FIELD_NO);
        xFieldRef := xRecRef.FIELD(YOUR_FIELD_NO);
    
        IF FieldRef.VALUE <> xFieldRef.VALUE THEN...
    
  • ThalonThalon Member Posts: 10
    Hi Addo,

    thanks for trying, but when used in the OnModify-Trigger this returns the new entry for xRec, too.
    Gettable does no "Transferfields", instead it checks the database and here the new value is returned :-(

    Lg,
    Thalon
  • vaprogvaprog Member Posts: 1,140
    Hi Thalon,

    your problem is probably not xRec, but Rec. In the OnModify trigger, the record has not yet been written to the database, so (assuming your explanation concerning GETTABLE is correct) xRecRef.GETTABLE(Rec) should give you access to xRec. But you are not able to get a RecRef with Rec's values.

    I suggest to do it the way Change Log Management (CU 1 / CU 423) does. There, NAV provides the RecRef to Rec as parameter to OnDatabaseModify (CU 1).


    Addendum
    Since I doubted Thalon's claim that RecRef.GETTABLE fetches values from the database, I made a quick test:
    I created a table with two fields, Code (ID 1) and Description (ID 2). Then I added the following code
    OnModify=VAR
      xRecRef@1100118001 : RecordRef;
      RecRef@1100118000 : RecordRef;
    BEGIN
      RecRef.GETTABLE(Rec);
      xRecRef.GETTABLE(xRec);
      MESSAGE('Old %1\New %2',xRecRef.FIELD(2).VALUE,RecRef.FIELD(2).VALUE);
    END;
    
    I created a record with Description 'Old', then changed it to 'New'. Here's the output of the Message
    Microsoft Dynamics NAV

    Old Old
    New New
    OK
    This test has been done on NAV 2013 R2, but I don't think the version matters in this regard.
  • ThalonThalon Member Posts: 10
    @vaprog
    Thanks for testing.
    I'm not exactly sure why my test failed, but your code works for me in NAV 2015.

    Edit:
    To get only the changed fields I wrote this function:
    LOCAL PROCEDURE GetModifiedFields@1100017008();
        VAR
          Field@1100017003 : Record 2000000041;
          xRecRef@1100017001 : RecordRef;
          RecRef@1100017005 : RecordRef;
        BEGIN
          RecRef.GETTABLE(Rec);
          xRecRef.GETTABLE(xRec);
          Field.RESET;
          Field.SETRANGE(TableNo,RecRef.NUMBER);
          Field.FINDSET(FALSE,FALSE);
          REPEAT
            IF RecRef.FIELD(Field."No.").VALUE <> xRecRef.FIELD(Field."No.").VALUE THEN
              MESSAGE('%1\%2\\%3\%4','xRecRef:',xRecRef.FIELD(Field."No.").VALUE,'RecRef:',RecRef.FIELD(Field."No.").VALUE);
          UNTIL Field.NEXT = 0;
        END;
    

    Thank you!
    Thalon
Sign In or Register to comment.