GETLASTERRORTEXT question

nvermanverma 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.
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

  • skullaskulla Member Posts: 140
    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 := GETLASTERRORTEXT
  • nvermanverma Member Posts: 396
    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.
  • skullaskulla Member Posts: 140
    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.
  • vaprogvaprog Member Posts: 1,140
    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.
    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:
    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.
    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.
Sign In or Register to comment.