Could anyone explain more about recordref

gisongison Member Posts: 128
I saw the Navision help, but still got confusing about this data type. :?
Could anyone give me some simple example for the best practice of recordref, or in any kind of situation we have to or better to use this data type.

Any advice would be appreciated.
Thank you.

Comments

  • kinekine Member Posts: 12,562
    RecordRef and FieldRef are used in situation, you need to process data from different tables with one code. It means e.g. ChangeLog - it is working on any table in the database, you cannot define new variable of type Record for each table you will use - th RecordRef is "dynamic" variable, which can work with any table. FieldRef than is the "dynamic" field access variable, which can access any table of the RecordRef (and thus every table in the DB).
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • tinoruijstinoruijs Member Posts: 1,226
    gison wrote:
    Could anyone give me some simple example for the best practice of recordref, or in any kind of situation we have to or better to use this data type.

    I've once made some extra discount-fields on the sales line, sales invoice line, return receipt line and sales credit memo line.
    I've kept the fieldnumbers in each table the same so TRANSFERFIELDS works for the new fields.

    Next a form "Discount Display Form" shows the new fields for all the tables by using RecordRef and FieldRef.
    See code below how it was applied:
    rrfSalesTable.OPEN(intTableID);
    CASE intTableID OF
      37: BEGIN;
            recSalesLine.GET(intDocType, codDocNo, intLineNo);
            rrfSalesTable.GETTABLE(recSalesLine);
            ldecLineAmountGross := recSalesLine.Quantity * recSalesLine."Unit Price";
          END;
      113: BEGIN;
             recSalesInvoiceLine.GET(codDocNo, intLineNo);
             rrfSalesTable.GETTABLE(recSalesInvoiceLine);
             ldecLineAmountGross := recSalesInvoiceLine.Quantity * recSalesInvoiceLine."Unit Price";
           END;
      6661: BEGIN;
              recReturnReceiptLine.GET(codDocNo, intLineNo);
              rrfSalesTable.GETTABLE(recReturnReceiptLine);
              ldecLineAmountGross := recReturnReceiptLine.Quantity * recReturnReceiptLine."Unit Price";
            END;
      115: BEGIN;
             recSalesCrMemoLine.GET(codDocNo, intLineNo);
             rrfSalesTable.GETTABLE(recSalesCrMemoLine);
             ldecLineAmountGross := recSalesCrMemoLine.Quantity * recSalesCrMemoLine."Unit Price";
           END;
      ELSE
        ERROR('To do');
    END;
    ldecLineAmountNet := ldecLineAmountGross;
    FOR lintCounter := 1 TO 6 DO BEGIN;
      lintFieldNo := 51100 + lintCounter - 1;
      frfSalesField := rrfSalesTable.FIELD(lintFieldNo);
      txtDescription[lintCounter] := frfSalesField.VALUE;
      IF txtDescription[lintCounter] <> '' THEN BEGIN;
        lintFieldNo := 51110 + lintCounter - 1;
        frfSalesField := rrfSalesTable.FIELD(lintFieldNo);
        decDiscountPercentage[lintCounter] := frfSalesField.VALUE;
        lintFieldNo := 51120 + lintCounter - 1;
        frfSalesField := rrfSalesTable.FIELD(lintFieldNo);
        decDiscountAmount[lintCounter] := frfSalesField.VALUE;
        decLineAmount[lintCounter] := ldecLineAmountNet;
        ldecLineAmountNet := ldecLineAmountNet - decDiscountAmount[lintCounter];
        decRemainingAmount[lintCounter] := ldecLineAmountNet;
      END;
    END;
    

    Advantage in this case:
    - only 1 form needed. In case of modifications, you only have to modify a single form.
    Disadvantage:
    - it's more difficult to create than a 'normal' form.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • jsnayberkjsnayberk Member Posts: 58
    gison wrote:
    I saw the Navision help, but still got confusing about this data type. :?
    Could anyone give me some simple example for the best practice of recordref, or in any kind of situation we have to or better to use this data type.

    Any advice would be appreciated.
    Thank you.

    See below an example which I recently wrote for an interface to an external application:

    IF NOT IsLogActive() THEN
    EXIT;
    CurrNo := InsertExportHeader(RecRef, oOperate::Update);
    FOR i := 1 TO RecRef.FIELDCOUNT DO BEGIN
    FldRef := RecRef.FIELDINDEX(i);
    IF IsNormalField(RecRef.NUMBER, FldRef.NUMBER) THEN
    InsertExportLine(FldRef, CurrNo);
    END;

    I think this shows how RecRef and FieldRef can work together.
    As kine said before, RecRef virtually can represent any record. So you are enabled to write really flexible code. Its just a higher level of abstraction.

    (InsertExportHeader, IsNormalField, InsertExportLine are (sub-)function and not declared here)
    --
    Josef Snayberk
  • gisongison Member Posts: 128
    Thanks Kine, tinoruijs and jsnayberk,

    I got to take some times to digest your helping.
    It's a different concept I have never heard.
    Are there have same data type in other programing language... :?
  • gisongison Member Posts: 128
    Hi Kine,
    As you said that change log example....probably I can refer to Codeunit "Change Log Management".InsertLogEntry function?

    Kind Regards,
    gison
  • gisongison Member Posts: 128
    Actually, I got a little bit understanding about why we need using RecordRef, but got confused in KeyRef, FieldRef, FieldIndex.......
    What does these exist for? When we get a table by RecordRef then do the modification for this table...What are the KeyRef, FieldRef, FieldIndex.... :?

    Sorry...for the troubling problem and thank you all your helping.
    :|
  • tinoruijstinoruijs Member Posts: 1,226
    Another usage of Recordref can be found in this post. Not huge, but effictive: http://www.mibuso.com/forum/viewtopic.php?t=24179

    For KeyRef, FieldRef, FieldIndex you could search this site (i.e. http://www.mibuso.com/forum/viewtopic.php?t=7767&highlight=keyref) or press F1 when you're in the C/AL Symbol Menu on a recordref variable.

    Tino Ruijs
    Microsoft Dynamics NAV specialist
  • gisongison Member Posts: 128
    dear tinoruijs,

    I appreciate your help. :wink:
    I'm figuring out.........
Sign In or Register to comment.