GETLASTERRORTEXT question

nverma
Member Posts: 396
Hi everyone,
I am trying to develop an Item Archive feature for one of our clients. Since they have lots of items that are no longer used, they wish to archive them so that the master Item list would only display items that are currently being used.
I created a batch report which will go through all the items and try to archive each of them as long as they are no longer being used in SO, PO, Item Journal Line, etc etc. If they are not being used I copy the Item record to Item Archive table using transferfields and I delete the Item record.
However, if the Item could not be deleted for whatever reason (i.e. its currently being used on a Sales Line), when I try to delete the Item system will error out due to the code in OnDelete trigger in the Item table. I need to be able to save this error message and display it in the report if the Item is not deleted. I am struggling to retrieve the error message using GETLASTERRORTEXT function. Could someone tell me how to archive this. The following is the code in the OnAfterGetRecord of the Item trigger.
Currently, when the system executes the Code in the OnDelete trigger of the Item, it errors out, rather then bypassing the error message and saving the error as a text.
I am trying to develop an Item Archive feature for one of our clients. Since they have lots of items that are no longer used, they wish to archive them so that the master Item list would only display items that are currently being used.
I created a batch report which will go through all the items and try to archive each of them as long as they are no longer being used in SO, PO, Item Journal Line, etc etc. If they are not being used I copy the Item record to Item Archive table using transferfields and I delete the Item record.
However, if the Item could not be deleted for whatever reason (i.e. its currently being used on a Sales Line), when I try to delete the Item system will error out due to the code in OnDelete trigger in the Item table. I need to be able to save this error message and display it in the report if the Item is not deleted. I am struggling to retrieve the error message using GETLASTERRORTEXT function. Could someone tell me how to archive this. The following is the code in the OnAfterGetRecord of the Item trigger.
Currently, when the system executes the Code in the OnDelete trigger of the Item, it errors out, rather then bypassing the error message and saving the error as a text.
CLEARLASTERROR; ItemArchive.SETRANGE("No.", "No."); IF ItemArchive.FIND('-') THEN CurrReport.SKIP ELSE BEGIN ItemArchive.INIT; ItemArchive.TRANSFERFIELDS(Item); ItemArchive.INSERT; Deleted := Item.DELETE(TRUE); IF Deleted THEN ErrorString := 'Sucessfully Archived!' ELSE BEGIN ErrorString := GETLASTERRORTEXT; END; END;
0
Answers
-
Hello,
Create a new codeunit and move you code into the OnRun with parameter Item
Move this code
ItemArchive.SETRANGE("No.", "No.");
IF ItemArchive.FIND('-') THEN
CurrReport.SKIP
ELSE
BEGIN
ItemArchive.INIT;
ItemArchive.TRANSFERFIELDS(Item);
ItemArchive.INSERT;
Then your code will look like
CLEARLASTERROR;
IF NewCodeunit.RUN(Item) THEN
ErrorString := 'Successfully Archived'
ELSE
ErrorString := GETLASTERRORTEXT0 -
Thanks Suresh.
The method you suggested worked. From a learning point of view, how did you figure it out. I looked in the Microsoft training manuals and partnersource and I could not find any information on how to use the GETLASTERRORTEXT message function.
Is there a book out there that has details explanation of functions/examples that are not explained by Microsoft.0 -
I just used this method several times that's how i remember it and i think there is an example in the Help i.e f1 or from other places where standard code uses.0
-
Well, this one is tricky (or not clearly documented).
Help on GETLASTERRORTEXT states (annotations mine)The text string that was contained in the last error message that was generated by Microsoft Dynamics NAV.
Help on DELETE (Record) states:Property Value/Return Value
Type: Boolean
true if the record was deleted; otherwise false. If the C/AL code terminates, then false is returned.
If you omit this optional return value and if the record is not deleted, then a run-time error occurs. If you include the return value, then you must handle any errors.
Help on RUN Function (Codeunit) states:Property Value/Return Value
Type: Boolean
If you do not include the optional return value and an error occurs while the codeunit is running, then the C/AL code that called this codeunit will terminate.
If you include the return value and an error occurs, then the calling C/AL code continues to run. This means that you must handle any errors. If you include the return value, the variables used in the codeunit will be cleared before and after the codeunit runs.
true if no errors occurred; otherwise, false.
So, the documentation is correct, just about nobody will have understood the thing you were struggling with without additional knowledge from other sources.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