Trap Error

alex9alex9 Member Posts: 97
Hi ,

I am trying to create a function( OR CodeUnit) that will catch Error in NAV and dump it in a Error Log Table. We want to use NAS, that will Post a Receipt for a Purchase order. Suppose that there was an error while posting, this Error should be inserted to a Error Log Table BUT not commit anything before then.. i.e - any other insert on any other table should fail.

e.g.
Suppose that you had
Table1.Insert();
Table2.Insert();
Table 3.Insert();

IF something=’’ THEN
IF NOT TrapError THEN
ERROR(‘ Something is blank’)
ELSE
BEGIN
WriteError(‘Somethig is blank’);
EXIT()
END;

I want WriteError to write to an Error Log but want the insert on Table 1,2 and 3 to fail. I believe that a commit on the WriteError Function will also insert on T1,T2, and T3 which is not desired.. Any thoughts? Or automation out there? I.e I don’t want to use a TXT File.

Comments

  • ara3nara3n Member Posts: 9,256
    You need a new codeunit that will call your codeunit

    It will call your codeunit in the following way.



    Onrun

    If not Codeunit.run() then begin
    WriteLog(getlastErrortext);
    clearlastErrorText;
    commit;
    end;
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • kinekine Member Posts: 12,562
    alex9 wrote:
    Hi ,

    I am trying to create a function( OR CodeUnit) that will catch Error in NAV and dump it in a Error Log Table. We want to use NAS, that will Post a Receipt for a Purchase order. Suppose that there was an error while posting, this Error should be inserted to a Error Log Table BUT not commit anything before then.. i.e - any other insert on any other table should fail.

    e.g.
    Suppose that you had
    Table1.Insert();
    Table2.Insert();
    Table 3.Insert();

    IF something=’’ THEN
    IF NOT TrapError THEN
    ERROR(‘ Something is blank’)
    ELSE
    BEGIN
    WriteError(‘Somethig is blank’);
    EXIT()
    END;

    I want WriteError to write to an Error Log but want the insert on Table 1,2 and 3 to fail. I believe that a commit on the WriteError Function will also insert on T1,T2, and T3 which is not desired.. Any thoughts? Or automation out there? I.e I don’t want to use a TXT File.

    I will NEVER go the way when you need to modify all calls of ERROR function etc. Because there are validate errors testfield errors etc. Ara3n's solution is much better, because it is generic and you do not need to modify existing code. It is working on NAV 5.0 and higher. If you are using older NAV, you can do technical upgrade or you can use another way, using NTimer automation which have OnError event to catch the error and write it. Than you only need to call the function for posting the document in OnTimer event for each document...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • alex9alex9 Member Posts: 97
    Thanks everyone for the reply.. I was wondering how you would “getLastErrorText” if the error message originated for the Codeunit.RUN(xx) and xx has just error out. Is the intention to modify CU xx also?
    If not Codeunit.run() then begin
    WriteLog(getlastErrortext);
    clearlastErrorText;
    commit;
    end;
    
  • ara3nara3n Member Posts: 9,256
    this is a new function added in 5.x version executables. The client keeps track of the error and you can retrieve it.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
Sign In or Register to comment.