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.
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;
Answers
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 := GETLASTERRORTEXT
ArcherPoint, Inc http://www.archerpoint.com
Blog: http://www.navisionworld.com
skkulla
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.
ArcherPoint, Inc http://www.archerpoint.com
Blog: http://www.navisionworld.com
skkulla
Help on GETLASTERRORTEXT states (annotations mine)
Help on DELETE (Record) states: note: nothing is said about an error beeing generated in case the return value is used. Instead it is stated that an error is "occurs" "If you omit this optional return value and ..."
Help on RUN Function (Codeunit) states: The phrase "an error occurs" implies, that an error had been generated.
So, the documentation is correct, just about nobody will have understood the thing you were struggling with without additional knowledge from other sources.