Error Handling on Validation

sabzamsabzam Member Posts: 1,149
Hi all,

im developing an import dataport. the scope of this dataport is to import new items. till this stage everything works correctly including validation. my problem is that i want to create a text file whenever the dataport find invalid data (ex. item already exists, or Inventory posting group does not exist)

Im using the Table.Validate function to insert data such as the inventory posting group. is there any way that i capture the error when it occurs on this function and pass it on in order to be written in the text file. (i am thinking the log file would have a simple structure such as : Item No. and a text message ex, inventory posting group does not exist)

my idea was to use the if statement and include the table.validate function within, but it does not return a boolean value. any ideas??

Thanks

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    In 5.0 you have
    GETLASTERRORTEXT
    
    under SYSTEM -> Error Handling.

    And for boolean return value you could use
    IF NOT CODEUNIT.RUN(yourCU_ID,[Rec]) THEN
      MESSAGE('%1',GETLASTERRORTEXT);
    
    and in OnRun
    Rec.VALIDATE("Your Field",NewValue);
    
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • BeliasBelias Member Posts: 2,998
    there is also a way to use a single codeunit for every error handling you need,the principle is the same as einsTeIn.NET
    said (e.g. i used this "trick" to handle the error returned from excel if an "excel find" fails, too)...i'm working on it, i will post it somewhere
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • sabzamsabzam Member Posts: 1,149
    thanks for the getlasterrortext, didnt know about that.

    im still unsure about the rest, i am not using any codeunit. please find below an extract of my code:

    IF (Item.GET(ItemTemp."No.")) THEN //if item is found generate an error message
    BEGIN
    "Write Error";
    //ERROR('Item Exists');
    END
    ELSE //else if item does not exist
    BEGIN

    Item."No." := ItemTemp."No."; //insert item no. and the required fields
    Item.INSERT;

    Item."Barcode No." := ItemTemp."Barcode No.";

    Item.Description := ItemTemp.Description;

    Item."Description 2" := ItemTemp."Description 2";

    "Create ItemUOM"(ItemTemp."No.",ItemTemp."Base Unit of Measure");
    Item.VALIDATE(Item."Base Unit of Measure",ItemTemp."Base Unit of Measure");

    "Create ItemUOM"(ItemTemp."No.",ItemTemp."Sales Unit of Measure");
    Item.VALIDATE(Item."Sales Unit of Measure",ItemTemp."Sales Unit of Measure");

    Item.VALIDATE(Item."Inventory Posting Group",ItemTemp."Inventory Posting Group");


    Write Error and Create ItemUOM are functions i wrote. as you can see from the first part i ama ble to write the item no in the log file if that does not exist. where im having a problem is with the other validate functions because i cannot insert any if else statements.
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    If you have so much validations you should think about to source out all into the codeunit.
    IF (Item.GET(ItemTemp."No.")) THEN                          //if item is found generate an error message
      BEGIN
        "Write Error";
        //ERROR('Item Exists');
      END
    ELSE                                                                 //else if item does not exist
      BEGIN
        Item."No." := ItemTemp."No.";                           //insert item no. and the required fields
        IF NOT CODEUNIT.RUN(yourCU_ID,Item) THEN
          MESSAGE('%1',GETLASTERRORTEXT);
      END;
    
    And in OnRun of your Codeunit something like that
    GetTempItem(var TempItem);
    Rec.INSERT;
    Rec."Barcode No." := ItemTemp."Barcode No.";
    Rec.Description := ItemTemp.Description;
    Rec."Description 2" := ItemTemp."Description 2";
    
    "Create ItemUOM"(ItemTemp."No.",ItemTemp."Base Unit of Measure");
    Rec.VALIDATE("Base Unit of Measure",ItemTemp."Base Unit of Measure");
       
    "Create ItemUOM"(ItemTemp."No.",ItemTemp."Sales Unit of Measure");
    Rec.VALIDATE("Sales Unit of Measure",ItemTemp."Sales Unit of Measure");
    
    Rec.VALIDATE("Inventory Posting Group",ItemTemp."Inventory Posting Group");
    
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • SavatageSavatage Member Posts: 7,142
    Seems to me your going to have to do something like you did to find if the item exists
    if InventoryPostingGroup.GET(ItemTemp."Inventory Posting Group") then
    VALIDATE(Item."Inventory Posting Group",ItemTemp."Inventory Posting Group")
    else "write error"
    
    you know what i mean.
  • sabzamsabzam Member Posts: 1,149
    yes savatage that was my choice to go around this problem. Thanks to all for your help.
Sign In or Register to comment.