Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Jesús Almaraz blog / About RecordRefs: Generic g...

About RecordRefs: Generic get last line number

Jalmaraz Profile Picture Jalmaraz 669

Avoid repetitive code

This last week I was strongly bored repeating a task: getting last number line in a document line record to insert a new one from a given document number: you know what I mean, we are creating a new sales line in an existing document, and you need to retrieve the last line no. and add 10000 to get the new line no.:
        with SalesLine do begin
            "Document Type" := SalesHeader."Document Type";
            "Document No." := SalesHeader."No.";
            "Line No." := GetSalesLineLastLineNo(SalesLine10000;
            Description := 'New text line';
            Insert();
        end;
And the function to get the Last line No., could be this way:
    local procedure GetSalesLineLastLineNo(SalesLine: Record "Sales Line")integer
    begin
        with SalesLine do begin
            SetRecFilter();
            SetRange("Line No.");
            if FindLast() then
                exit("Line No.");
        end;
    end;

Generic record managers

We have a set of datatypes from many years ago to manage records, fields and keys in generic way: RecordRef, FieldRef and KeyRef:
They are mainly used for these purposes:
  • Developer tools.
  • Data transfers.
  • Test code libraries. I talked previously about test code crazy stuff and the Recordref ones are my favorite. The next example is taken from test code libraries, but I simplified the original code.

Example: implementing generic last number

    procedure GetLastLineNo(MyRecord: Variant)Integer
    var
        RecRef: RecordRef;
        PrimaryKey: KeyRef;
        LastFieldPK: FieldRef;
        LastFieldNoPK: Integer;
    begin
        RecRef.GetTable(MyRecord);
        RecRef.SetRecFilter();
        PrimaryKey := RecRef.KeyIndex(1);
        LastFieldNoPK := PrimaryKey.FieldIndex(PrimaryKey.FieldCount).Number;
        LastFieldPK := RecRef.Field(LastFieldNoPK);
        LastFieldPK.SetRange();
        if RecRef.FindLast() then
            exit(LastFieldPK.Value);
    end;
A quick breakdown. The input parameter is a variant, and we expect and specific record as shown below:
        with SalesLine do begin
            "Document Type" := SalesHeader."Document Type";
            "Document No." := SalesHeader."No.";
            "Line No." := GetLastLineNo(SalesLine10000;
We load all the specific record (Sales Line) state, fields and filters, in the Recordref variable with GETTABLE statement.
With SETRECFILTER statement we set the filter to the current values of the primary key fields. Then we remove the filter of last field of the primary key with a KEYREF and a FIELDREF Data types.

Final

This function in a Utility library could avoid the need of repeat all the time the code of retrieving last Line No. or Entry No. But I have two objections to the use of this code:
  • BC standard code avoid the use of these data type in final user apps.
  • I don´t want utility library, unless BC standard provide them to me.
But I'm so bored of repeating get last line number, that I included this code in my project.

Comments

*This post is locked for comments