Codeunit.Run no working when triggered from event subscriber

samantha73
Member Posts: 118
Hi All
I have a requirement to copy fields from one record to another - not a simple copy so cannot use transferfields. Something like sales order to Sales Invoice transfer of fields. I'm finding it difficult find a trigger for below
[EventSubscriber(ObjectType::Table, Database::"Service Item Line", OnAfterValidateEvent, My FIeld, false, false)]
local procedure TransferToCalibrationHeader(var Rec: Record "Service Item Line"; var xRec: Record "Service Item Line")
.....
if not Codeunit.Run(Codeunit::MyCodeunit, Rec) then begin
this works if I run same codeunit to run from the page action as opposed to event sub which hints that something to do with transaction locking or values not full committed etc issue. Is there a better way to capture a event when everything is committed. We cannot ask the user to trigger this like sales-post or job queue as we need immediate access to the copied record. How do others deal with this kind of situation?
I have a requirement to copy fields from one record to another - not a simple copy so cannot use transferfields. Something like sales order to Sales Invoice transfer of fields. I'm finding it difficult find a trigger for below
[EventSubscriber(ObjectType::Table, Database::"Service Item Line", OnAfterValidateEvent, My FIeld, false, false)]
local procedure TransferToCalibrationHeader(var Rec: Record "Service Item Line"; var xRec: Record "Service Item Line")
.....
if not Codeunit.Run(Codeunit::MyCodeunit, Rec) then begin
this works if I run same codeunit to run from the page action as opposed to event sub which hints that something to do with transaction locking or values not full committed etc issue. Is there a better way to capture a event when everything is committed. We cannot ask the user to trigger this like sales-post or job queue as we need immediate access to the copied record. How do others deal with this kind of situation?
0
Best Answer
-
Your OnAfterValidate event subscriber is triggered mid-transaction, meaning the record hasn't been fully saved or committed yet. If your codeunit tries to read that record (especially from another context or table), it may face issues like:
Incomplete data
Locking or stale reads
Missing related data (e.g., header-line relationships not yet set)
Use OnAfterInsert or OnAfterModify instead, as these events occur later in the transaction when more data is available.Developer by passion0
Answers
-
Did you try OnAfterModify trigger? what is the exact error?0
-
Do you intent to modify the record after or before it is saved to the database?0
-
Your OnAfterValidate event subscriber is triggered mid-transaction, meaning the record hasn't been fully saved or committed yet. If your codeunit tries to read that record (especially from another context or table), it may face issues like:
Incomplete data
Locking or stale reads
Missing related data (e.g., header-line relationships not yet set)
Use OnAfterInsert or OnAfterModify instead, as these events occur later in the transaction when more data is available.Developer by passion0 -
OnAfterInsert/OnAfterModify sounds more practical as per the information and requirement you have posted. Did you try these Events?Thanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/1
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