insert sql error

geronimogeronimo Member Posts: 90
Is there a way to catch an sql insert error in nav code from outside the function?


a little example (just to make things clear):
i have 2 tables
both have this setup
1 code code 10
2 description text 30

All i do is copy the data over from one table to the other like this:
Fct_MoveData()
LRec_1.RESET;
IF LRec_1.FINDSET(FALSE,FALSE) THEN BEGIN
  REPEAT
    LRec_2.RESET;
    LRec_2.TRANSFERFIELDS(LRec_1);
    LRec_2.INSERT(TRUE);

  UNTIL LRec_1.NEXT = 0;
END;

However when i run the codeunit the second time i get this error

Microsoft Dynamics NAV Classic
Tabel testtable2 is reeds aanwezig.

Velden en waarden:

code='01'

OK

Which is expected as it should occur. I know i can write an "if" around it to catch the error. But i dont want to go around writing if's all over the database to then throw my own error that i can log with the getlasterrortext function.

So i'd like to know if there is a way to catch this without having to modify all over the place, as mentioned before the example above is merely for posting an example.

Comments

  • ara3nara3n Member Posts: 9,256
    yes, you create a CU that calls this function on OnRun trigger.


    MyCU5000

    onRun

    Fct_MoveData();



    Then you create another CU and call it

    if MyCU5000.run then begin
    message('success');
    end else begin
    logtheerror(getlasterrortext);
    clearlasterrortext;
    end;
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • geronimogeronimo Member Posts: 90
    ara3n wrote:
    yes, you create a CU that calls this function on OnRun trigger.


    MyCU5000

    onRun

    Fct_MoveData();



    Then you create another CU and call it

    if MyCU5000.run then begin
    message('success');
    end else begin
    logtheerror(getlasterrortext);
    clearlasterrortext;
    end;
    which is what i did. it still gives me the sql error (tried both defining the codeunit as a variable as the codeunit.run function)

    for example:

    2 codeunits:
    60000:
    Documentation()
    
    OnRun()
    
    IF COdeunit60001.RUN THEN;
    //IF CODEUNIT.RUN(60001) THEN;
    MESSAGE('test: %1',GETLASTERRORTEXT);
    

    Codeunit 60001
    Documentation()
    
    OnRun()
    
    Fct_MoveData()
    LRec_1.RESET;
    IF LRec_1.FINDSET(FALSE,FALSE) THEN BEGIN
      REPEAT
        LRec_2.RESET;
        LRec_2.TRANSFERFIELDS(LRec_1);
        LRec_2.INSERT;
    
      UNTIL LRec_1.NEXT = 0;
    END;
    
  • ara3nara3n Member Posts: 9,256
    Are you getting a sql error or NAV Error? and you are not getting the message 'test: The error?'

    Do you have any code on sql trigger?

    I have seen this before where an insert error would stop the execution of the calling CU.

    I suggest to report it to MS if you can replicate it in Cronus. probably newest executable.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • geronimogeronimo Member Posts: 90
    ara3n wrote:
    Are you getting a sql error or NAV Error? and you are not getting the message 'test: The error?'

    Do you have any code on sql trigger?

    I have seen this before where an insert error would stop the execution of the calling CU.

    I suggest to report it to MS if you can replicate it in Cronus. probably newest executable.
    I'll have a look in a standard database. I just tried it out in an empty database. (new database,new company, nothing in it but the two tables and the codeunits)
    no code on sql trigger.
    I'm not getting the message. It stops at the insert when i enable the debugger.

    Will have a look in cronus and come back when i have the results :)

    Thanks so far already
Sign In or Register to comment.