Add validation code in OnValidate code of existing field by extension

Hi Experts,

I was using AL for creating new table/page by extension.
I came across a requirement where I want to add an additional validation code in OnValidate trigger of a field in Item table by extension.
Can you please suggest as I kind of got stuck.

Regards

Answers

  • BlackTigerBlackTiger Posts: 1,144Member
    You can't as far as I understand.
    "You can’t just ask customers what they want and then try to give that to them.
    By the time you get it built, they’ll want something new.” Steve Jobs
  • vaprogvaprog Posts: 837Member
    BlackTiger wrote: »
    You can't as far as I understand.

    Really? I have not done anything with AL, but since events OnBeforeValidateEvent and OnAfterValidateEvent are there to be used from within C/AL, I suppose they must be there from AL also. In C/AL you get an additional property to choose the field, once you have chosen one of the two events mentioned before.

    HTH
  • JuhlJuhl Posts: 556Member
    Of course you can!

    Ex. Customer table

    tableextension 70050090 CustomerExtension extends Customer
    {
    fields
    {
    modify(Name) // Modify the existing name field.
    {
    // Customizable properties can be changed here.
    CaptionML = ENU = 'My Changed Caption';

    trigger OnBeforeValidate();
    begin
    // Before validating customer name
    end;

    trigger OnAfterValidate();
    begin
    // After validation customer name
    end;
    }
    }
    }
    Follow me on my blog juhl.blog
  • BlackTigerBlackTiger Posts: 1,144Member
    edited 2018-10-12
    It depends. OnBeforeValidate/OnAfterValidate can be not enough if you have to put a couple of lines into the field validation code (inside some IF/CASE to handle new Option value when code after case depends on case statement result). I can give you a lot of examples of real life situations when 'events' don't work.
    Let's create a new option for "Type" field in "Sales Line" and change "No."'s validation code.

    PS: Ok, Microsoft! Prove me wrong!
    "You can’t just ask customers what they want and then try to give that to them.
    By the time you get it built, they’ll want something new.” Steve Jobs
  • JuhlJuhl Posts: 556Member
    True. But NOT the question :-)
    Follow me on my blog juhl.blog
  • BlackTigerBlackTiger Posts: 1,144Member
    edited 2018-10-12
    Juhl wrote: »
    Of course you can!

    Just read original post:
    I want to add an additional validation code in OnValidate trigger".

    "You can’t just ask customers what they want and then try to give that to them.
    By the time you get it built, they’ll want something new.” Steve Jobs
  • JuhlJuhl Posts: 556Member
    Doesn’t say where.
    OnBefore or After usually is enough for most validation tasks. Like add extra validation.
    Follow me on my blog juhl.blog
  • nav013nav013 Posts: 2Member
    You could also create an event subscriber codeunit.
    And a function with something like this:

    codeunit 50000 MyEventsubscribers
    {
    [EventSubscriber(ObjectType::Table, Database::Item, 'OnAfterValidateEvent', 'Description 2', false, false)]
    local procedure OnAfterValidateEvent(VAR Rec: Record Item; VAR xRec: Record Item)
    var
    begin

    end;
    }
  • navbeginnernavbeginner Posts: 17Member
    Thanks a lot experts for your valuable advice.
    I will try to execute your advice and revert
Sign In or Register to comment.