Event Sequence - update field on modify and insert

markcle
Member Posts: 65
Hi All
Finalizing my first extension and stuck on one thing.
For certain shipping methods we have to add a freight line (non inventory item) and calc freight as percent of total order value. The percent is a unit price for each customer (e.g. 0.1 = 10%) and we put the order total in qty, so if Order is $1000, freight is $100
I have a subscription to change in Ship method and can insert the line with correct Qty (order total)
I also have a subscription on sales lines, so that if a sales line amount changes and there is a freight line, it will recalculate the total (calls a proc in a code unit)
I have one use case that doesn't work: The user inserts all line items and at the end, MANUALLY adds the freight line
I subscribed to the SalesLineAfterInsert, which calls my proc in the codeunit
Here's my Proc
I create a new line, enter the item no and hit tab, the proc runs, and all the calculations seem to work correctly, but the cursor is left in the Qty field (next column) and but the qty is not populated.
In most other languages, I would just use the OnLoseFocus event and populate the Qty field.
Can someone point me in the right direction or how to populate a filed of a newly entered record?
Thanks
mark
Finalizing my first extension and stuck on one thing.
For certain shipping methods we have to add a freight line (non inventory item) and calc freight as percent of total order value. The percent is a unit price for each customer (e.g. 0.1 = 10%) and we put the order total in qty, so if Order is $1000, freight is $100
I have a subscription to change in Ship method and can insert the line with correct Qty (order total)
I also have a subscription on sales lines, so that if a sales line amount changes and there is a freight line, it will recalculate the total (calls a proc in a code unit)
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterModifyEvent', '', true, true)]
I have one use case that doesn't work: The user inserts all line items and at the end, MANUALLY adds the freight line
I subscribed to the SalesLineAfterInsert, which calls my proc in the codeunit
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterInsertEvent', '', true, true)] var // Assign SLO variable to codeunit of SalesLineOperarions SLO: Codeunit SalesLineOperations; OrderTotal: decimal; begin // Check if this is a FREIGHT line if rec."No." = 'FCOM' then begin SLO.UpdateFCOMQty(rec); end; end;
Here's my Proc
procedure UpdateFCOMQty(var Rec: Record "Sales Line") // This is called whenever a sales line is modified or inserted // Checks if there's an FCOM record // If there is one, sum all the non-freight lines before tax and set this as QTY on FCOM var OrderTotal: decimal; FreightSalesLine: record "Sales Line"; begin // See if there are any FCOM lines. if so, calc total before tax and freight FreightSalesLine.SETRANGE("Document Type", Rec."Document Type"); FreightSalesLine.SETRANGE("Document No.", Rec."Document No."); FreightSalesLine.SETRANGE("No.", 'FCOM'); // If we have an FCOM line, calc the total of order minus freight If FreightSalesLine.Find('-') then begin OrderTotal := CalcOrderTotalWithoutTaxAndFreight(rec."Document No.", Rec."Document Type"); FreightSalesLine.Validate(Quantity, Round(OrderTotal, 1)); FreightSalesLine.Modify(TRUE); end; end;
I create a new line, enter the item no and hit tab, the proc runs, and all the calculations seem to work correctly, but the cursor is left in the Qty field (next column) and but the qty is not populated.
In most other languages, I would just use the OnLoseFocus event and populate the Qty field.
Can someone point me in the right direction or how to populate a filed of a newly entered record?
Thanks
mark
0
Best Answer
-
Hi, onafterinsert does not mean your record is committed to the database, or it even being present during runtime. The inserting is not ready at this point. OnAfterInsert refers to the place to put your code (being able to insert code before or after the standard oninsert trigger code).
Change parameters of CalcOrderTotalWithoutTaxAndFreight to a record var for your Rec Sales Line, and pass it. You can still do what you did in there before using "Document No" and "Document Type", but you can add values of your new record to the caculation of OrderTotal too.
5
Answers
-
Hi, onafterinsert does not mean your record is committed to the database, or it even being present during runtime. The inserting is not ready at this point. OnAfterInsert refers to the place to put your code (being able to insert code before or after the standard oninsert trigger code).
Change parameters of CalcOrderTotalWithoutTaxAndFreight to a record var for your Rec Sales Line, and pass it. You can still do what you did in there before using "Document No" and "Document Type", but you can add values of your new record to the caculation of OrderTotal too.
5 -
What I would do is create a function at the line level that creates/updates the related charges, and provide the ability to run that from the functions menu on the line. I'd also add a function at the header level so I can run it for all lines. Then, I would add an additional call to this function from the release process to make sure that orders cannot be released without those additional charges.
I would personally stay away from database triggers, because it is unpredictable what happened when a line was inserted/modified. The user can move the cursor up and down when just selecting the line type, or just selecting the item number without having a quantity/uom/price. Any amount calculation will be false at that time anyway.0 -
thanks for replies. TallyHo's answer worked (did actually figure it out myself :-))
Thanks again
Mark0
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