[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post (Yes/No)", OnBeforeConfirmSalesPost, '', true, true)] local procedure SetDefaultToShip(var SalesHeader: Record "Sales Header"; var DefaultOption: Integer) var begin if SalesHeader.IsTemporary then exit; if SalesHeader."Document Type" <> SalesHeader."Document Type"::Order then exit; SalesHeader.Reset(); [b][b]SalesHeader.SetRange("No.", SalesHeader."No.");[/b]//Why this line[/b] IF SalesHeader.FindFirst() THEN begin Message('Order No %1', SalesHeader."No.");
Answers
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.