"IF Validate" Function

BeliasBelias Member Posts: 2,998
edited 2009-02-09 in NAV Tips & Tricks
with this codeunit, you can manage the possible validate errors (like overflow etc).
OBJECT Codeunit 90000 If Validate Mgt
{
  OBJECT-PROPERTIES
  {
    Date=09/02/09;
    Time=12.23.59;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            FNTValidate;
          END;

  }
  CODE
  {
    VAR
      INTFieldNo@1101339002 : Integer;
      TXTNewValue@1101339001 : Text[250];
      FRFieldRef@1101339004 : FieldRef;
      RRRecordRef@1101339000 : RecordRef;

    PROCEDURE FNTValidate@1101339001();
    BEGIN
      IF TXTNewValue = '' THEN BEGIN
        FRFieldRef.VALIDATE;
      END ELSE BEGIN
        EVALUATE(FRFieldRef,TXTNewValue);
        FRFieldRef.VALIDATE;
      END;
    END;

    PROCEDURE FNTGetPar@1101339003(INTLocTableNo@1101339000 : Integer;INTLocFieldNo@1101339001 : Integer;TXTLocPosition@1101339002 : Text[1000];TXTLocValue@1101339003 : Text[250]);
    BEGIN
      TXTNewValue := TXTLocValue;
      RRRecordRef.OPEN(INTLocTableNo);
      RRRecordRef.SETPOSITION(TXTLocPosition);
      RRRecordRef.FIND('=');
      FRFieldRef := RRRecordRef.FIELD(INTLocFieldNo);
    END;

    PROCEDURE FNTGetTableNo@1101339000(TXTTableName@1101339001 : Text[50]) : Integer;
    VAR
      TBObject@1101339000 : Record 2000000001;
    BEGIN
      TBObject.SETRANGE(Type,TBObject.Type::Table);
      TBObject.SETRANGE(Name,TXTTableName);
      TBObject.FINDFIRST;
      EXIT(TBObject.ID);
    END;

    BEGIN
    END.
  }
}

Here's an example on how to use this codeunit:
OBJECT Codeunit 66666 getpos
{
  OBJECT-PROPERTIES
  {
    Date=09/02/09;
    Time=12.27.22;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            txtconst := 'mlallalllllflljfdshalfjadskfljadslfkjadslfjhadslfjhadsfhsadlkfhdslafhekhfuniciabernciebhrigvbiero';
            tbtest.FINDLAST;
            cu.FNTGetPar(cu.FNTGetTableNo(tbtest.TABLENAME),tbtest.FIELDNO(Name),tbtest.GETPOSITION(FALSE),txtconst);
            IF cu.RUN THEN BEGIN
              tbtest.VALIDATE(Name,txtconst);
              tbtest.MODIFY;
            END ELSE BEGIN
              MESSAGE('What customer is this?');
            END;
          END;

  }
  CODE
  {
    VAR
      tbtest@1101339000 : Record 18;
      rref@1101339001 : RecordRef;
      cu@1101339002 : Codeunit 90000;
      txtconst@1101339003 : Text[200];

    BEGIN
    END.
  }
}
Maybe there are problems with transactions, codeunit.run and "complex" validates...not tried yet...for sure, you should know what you are validating before using this function. :mrgreen:

*edit:
a possible use of this codeuint is when you are batch processing something: you can use getlasterrortext function to create a simple log if the validate goes error...
Enjoy!
-Mirko-
"Never memorize what you can easily find in a book".....Or Mibuso
My Blog
Sign In or Register to comment.