OnAfterValidate fail if Modify(true) on purchase line - line does not exist error

samantha73
Member Posts: 118
Hi Guys
I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't work
Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted
I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't work
modify(Quantity) { trigger OnAfterValidate() var PurchHeader: Record "Purchase Header"; begin PurchHeader.Reset(); PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order); PurchHeader.SetRange("No.", Rec."Document No."); If PurchHeader.Findfirst() then Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID); //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE end; }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]
Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted
0
Best Answer
-
samantha73 wrote: »Hi Guys
I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't workmodify(Quantity) { trigger OnAfterValidate() var PurchHeader: Record "Purchase Header"; begin PurchHeader.Reset(); PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order); PurchHeader.SetRange("No.", Rec."Document No."); If PurchHeader.Findfirst() then Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID); //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE end; }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]
Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted
You are getting the error because the record doesn't exist yet. Purchase Order Subform has "DelayedInsert = true" property, meaning the record is not actually inserted until you leave the line.
Your solutions would be to either check if the line exists first before modifying (if it doesn't exist, then no need to modify), or use OnBeforeValidate event and not modify at all.0
Answers
-
Hi,
I think you are doing it wrong.
You have to subscribe to an event subscriber, not "modify" quantity field which I think modifies the behaviour, something like:[EventSubscriber(ObjectType::Table, database::"Purchase Line", 'OnAfterValidateEvent', 'Quantity', false,false)] local procedure MyProcedure() begin end;
0 -
Both methods can be applied in this kind of scenarios with pros and cons but the question was specifically why wasn't the line inserted when using Quantity field trigger ?0
-
samantha73 wrote: »Hi Guys
I have this code on purchase line Onvalidate on quantity field as a table extension on purchase line table. With Modify statement it doesn't workmodify(Quantity) { trigger OnAfterValidate() var PurchHeader: Record "Purchase Header"; begin PurchHeader.Reset(); PurchHeader.SetRange("Document Type", PurchHeader."Document Type"::Order); PurchHeader.SetRange("No.", Rec."Document No."); If PurchHeader.Findfirst() then Rec.Validate(MY_AssetID, PurchHeader.MY_AssetID); //[b]Modify(true);[/b]//*LINE DOES NOT EXIST ERROR AS LINE NOT INSERTED AT THIS POINT IF NEW LINE end; }[img]https://us.v-cdn.net/5022383/uploads/editor/wd/99u184yecjmk.png[/img]
Not sure why the line does not exist as quantity is not one of the primary keys? When Modiy statement is commented out it works but trying to understand at what point the line is inserted
You are getting the error because the record doesn't exist yet. Purchase Order Subform has "DelayedInsert = true" property, meaning the record is not actually inserted until you leave the line.
Your solutions would be to either check if the line exists first before modifying (if it doesn't exist, then no need to modify), or use OnBeforeValidate event and not modify at all.0 -
Thanks heaps ..totally forgot the delayed insert ..learn something really useful0
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