Wednesday, January 30, 2019

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
class SaleLine_Events
{

    [PostHandlerFor(tableStr(SalesLine),tableMethodStr(SalesLine, validateField))]
    public static void SalesLine_Post_validateField(XppPrePostArgs args)
    {
        SalesLine salesLine = args.getThis();
        FieldId fieldId = args.getArg("_fieldId");
        boolean ret = args.getReturnValue();

        switch(fieldId)
        {
            case fieldNum(SalesLine,LinePercent):
                if (salesLine.LinePercent > 10)
                {
                    ret = ret && checkFailed("Line per cent is to high!");
                }
                break;
        }

        args.setReturnValue(ret);
    }

}

Here I set a few new variables. First of all I called the getThis() method on the passed argsargument.
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.
public boolean validateField(FieldId _fieldId)
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.

No comments:

Post a Comment

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 ...