AX7 / D365 Extensions: Extend the validateField Method on a Table

In this post I will going to show you, how to extend the validateField method on a table. As example I used the SalesLine, where I will create a post handler to extend the validateField method.

This kind of extensions were already there to be used in AX 2012, but since we have to work only with extensions now, events got a lot more focus nowadays.

Let’s go: ValidateField Extension

First search the SalesLine table in the AOT, then right click the table and select “Open designer”.

AX7 Extension Validate Field Designer

Go to the methods node, to the validateField method, right click, “Copy event handler method” and select “Post-event handler”.

AX7 Extension Validate Field Copy Handler

Now create a new class and paste the copied event handler into it.

Here I set a few new variables. First of all I called the getThis() method on the passed args argument.

The actual field to validate is more than interesting for us, for this reason we need to get the passed argument from the original method. In the AOT find the SalesLine again, right click it and select “View code”.

AX7 Extension Validate Field View Code

Search for the validateField method and you get the original argument’s name.

With getArg(“_fieldId”) the original value will be retrieved. ¬†To decide if a field is valid or not, the original boolean value has to be known (getReturnValue()) and also a new return value has to be set (setReturnValue(ret)), with our own validation.

Result

In the example above the per cent per line is checked, if it is greater than 10, it will return false and post a message to the infolog.

AX7 Extension Validate Field Change Field

Here I entered 11.

AX7 Extension Validate Field Result

And my warning is shown.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *