Error Handling

schoolkidschoolkid Member Posts: 103
Dear all,

need your help on this.

i have a sales buffer table in which i populate sales orders and then move them to sales header table.when the data in the buffer table has some errors it has to mark the particular record as error and move on to the next record.

i wrote the following code but seems that it doesnt work



IF "Sales Header Buffer".FIND('-')THEN BEGIN
REPEAT

"Sales Table".INIT;

"Sales Table"."Document Type" := "Sales Header Buffer"."Document Type";
"Sales Table"."No." := "Sales Header Buffer"."No.";
"Sales Table"."Sell-to Customer No." := "Sales Header Buffer"."Sell-to Customer No.";
"Sales Table".VALIDATE("Sales Table"."Document Date",TODAY);

"Sales Table".LOCKTABLE;
IF NOT("Sales Table".INSERT)
THEN BEGIN
"Sales Header Buffer"."Error":= TRUE;
"Sales Header Buffer".MODIFY(TRUE);
END
ELSE begin
message('Imported Successfully';)
END;
COMMIT;
UNTIL "Sales Header Buffer".NEXT = 0;
END;


any help on this please

Comments

  • kinekine Member Posts: 12,562
    You cannot do it in this way. If you are using NAV 5.0SP1, you can use the construction
      if codeunit.run(myrecord) then begin
         //Ok
      end else begin
        //Error
        MESSAGE('%1',GETLASTERRORTEXT);
      end;
    

    You need to insert the code into the codeunit OnRun trigger (or just calling some function from there). If there is any error in the CU, it will return false, if all is ok, it will return true and you can catch these and get the error text to save it somewhere when needed. Just do not forget that it will run as separate transaction...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • schoolkidschoolkid Member Posts: 103
    Hi,

    i am using NAV 2009
  • SunsetSunset Member Posts: 201
    Kine's suggestoin also works for NAV 2009
    Don't just take my word for it, test it yourself
  • hawkeyehawkeye Member Posts: 51
    or just

    IF NOT "Sales Table".INSERT(TRUE) then BEGIN
    .....

    When inserting with true - you tricker the insert trigger.
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    If you don't use the Codeunit.RUN construction you will have a break on your loop if there is an error within the OnInsert trigger or the OnValidate trigger of a certain field. In that case you can't mark your record.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • BeliasBelias Member Posts: 2,998
    moreover, the "if not insert/if insert" construction is not optimal...but this is only the "cherry on the cake" :mrgreen:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • schoolkidschoolkid Member Posts: 103
    thank u all for your help,i got it solved.
    i used the codeunit.run() and this was the best way i could achieve it.

    thanks again :thumbsup:
Sign In or Register to comment.