Event Sub filer record to current record logic

samantha73
Member Posts: 118
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:
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) 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.");
0
Answers
-
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions