how can i retreive error messages

spiky_goldenspiky_golden Member Posts: 44
I have a code unit that insert data into a custom table. I Need to retreive and log all error occuring when Insert command fail. I Use a line taht looks like that :
IF NOT  tmpRecord.INSERT THEN  BEGIN

Is there any way to get the error message why the code-unit goes into this condition.
Patrick Venner
programmeur-analyste
Cabico Custom Cabinetry

Answers

  • BBlueBBlue Member Posts: 90
    Hello,

    If you are on 5.0 you can use a construct like the following to catch and log the eventual error caused by not inserting the record:
    IF NOT CODEUNIT.RUN(Codeunit::"The codeunit that inserts the record") THEN BEGIN
      IF GETLASTERRORTEXT <> '' THEN
      //save the error returned by GETLASTERRORTEXT into a table, for example
      CLEARLASTERROR;
    END;
    

    Regards
    //Bogdan
  • spiky_goldenspiky_golden Member Posts: 44
    Here's the code i use
              IF NOT tmpRecord.INSERT THEN  BEGIN
                Window.UPDATE(2,'NOT DONE');
                gOutStream.WRITETEXT( 'DealerId = ' + PadIt(FORMAT(tmpRecord."No."),5) + 
                                ' ; DealerName = ' +tmpRecord.Name  + ' ; Error Code  = ' + GETLASTERRORTEXT);
                gOutStream.WRITETEXT();
                CLEARLASTERROR();
             END
    
    
    I always get GETLASTERRORTEXT = ''
    but as far as I know if it goes in the IF condition it should have an error, right?
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • BBlueBBlue Member Posts: 90
    No, as far as I know GETLASTERRORTEXT only works in the construction "If Codeunit.RUN() THEN...".
    Have you tried to modify your code to use this context?
    //Bogdan
  • kinekine Member Posts: 12,562
    BBlue wrote:
    No, as far as I know GETLASTERRORTEXT only works in the construction "If Codeunit.RUN() THEN...".
    Have you tried to modify your code to use this context?

    And to have correct error, the error must be fired, it means the INSERT must be used without return value (without the IF), else there is no error thus no error message in the GETLASTERRORTEXT...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • spiky_goldenspiky_golden Member Posts: 44
    Well I'm pretty new in NAV... How can I use this kind of context? Should I make a new codeUnit with only the insert command, and then call it and check the error?
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • BBlueBBlue Member Posts: 90
    How can I use this kind of context? Should I make a new codeUnit with only the insert command, and then call it and check the error?

    That's correct. Or use the current one and change the logic, but do not use "if not insert then...", just "record.insert;...".
    Then call this codeunit with the construction "If codeunit.run() THEN..."
    //Bogdan
  • spiky_goldenspiky_golden Member Posts: 44
    Well thank you I managed to make it work like this
    CLEARLASTERROR;
    gWriteCodeUnit.Init(tmpRecord);
    
    IF NOT gWriteCodeUnit.RUN THEN
         BEGIN
              Window.UPDATE(2,'NOT DONE');
              gOutStream.WRITETEXT( 'DealerId = ' + PadIt(FORMAT(tmpRecord."No."),5) + 
                                ' ; DealerName = ' +tmpRecord.Name  + ' Error Code  = ' + GETLASTERRORTEXT);
              gOutStream.WRITETEXT();
         END;
    

    Where the code in the gWriteCodeUnit looks like :
    Init(RecordSet : Record "Test Import")
    WriteRecords := RecordSet;
    
    ON RUN() 
    WriteRecords.INSERT;
    

    Maybe it's not pretty but it work just fine ! :D
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • kinekine Member Posts: 12,562
    You can do it better when you set the table as SourceTable on the codeunit (look into codeunit properties) and after that you can pass the record directly in Codeunit.RUN as parameter... :wink:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • spiky_goldenspiky_golden Member Posts: 44
    How can you pass the record directly to the run command? The only Way I have found is by using a function...
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
  • kinekine Member Posts: 12,562
    Read my answer once again... ;-)
    look into codeunit properties

    Open the codeunit in designer, press Shift+F4... TableNo... :wink:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • spiky_goldenspiky_golden Member Posts: 44
    Well it's great this way! Everything works just fine!

    Thanks
    Patrick Venner
    programmeur-analyste
    Cabico Custom Cabinetry
Sign In or Register to comment.