Event Sub filer record to current record logic

samantha73samantha73 Member Posts: 94
edited 2023-09-27 in NAV Three Tier
Hi All
I'm subscribing to the Sales-Post codeunit and trying to create a validation based on the current document a user is interacting with. I cannot understand the reason for the below highlighted line of code to filter the document no as I assume SalesHeader already is filtered to the current record (SalesHeader.SetRange("No.", SalesHeader."No.") on user screen:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post (Yes/No)", OnBeforeConfirmSalesPost, '', true, true)]
    local procedure SetDefaultToShip(var SalesHeader: Record "Sales Header"; var DefaultOption: Integer)
        if SalesHeader.IsTemporary then
        if SalesHeader."Document Type" <> SalesHeader."Document Type"::Order then
        [b][b]SalesHeader.SetRange("No.", SalesHeader."No.");[/b]//Why this line[/b]
        IF SalesHeader.FindFirst() THEN begin
            Message('Order No %1', SalesHeader."No.");


  • vaprogvaprog Member Posts: 1,100
    Well, you can assume - and fail - or verify and or assert by bringing about what you assume.

    You should use SETRECFILTER instead though, or at least check the "Document Type" after the FINDFIRST. No-one guarantees you document numbers to be unique to the document type, and no-one guarantees you the values in the record's fields to match the filters applied to it, until after you read that record from the database with those filters in place.

    And since you mess with the filters, and you use a VAR parameter for your record, you should consider doing your checks without needing to change the filter nor fetching it fresh from the database, or do the checks on a copy which you GET from the database or FIND after COPYFILTERS and filter modifications as needed. Or else make sure the caller is ready to accept such changes (which is much easier done for an event-subscriber than for a regular subroutine.
Sign In or Register to comment.