Friday 8 November 2019

VALIDATEFIELD CODE example in D365 ax 2012 EVENT HANDLER

VALIDATE FIELD CODE IN D365

Validate Field is called whenever a value is changed at the field level.
For eg. if you write the discount value as 101 you get the error.This error is getting called from the validate field.

public boolean validateField(FieldId _fieldIdToCheck)
{
boolean ret;

ret = super(_fieldIdToCheck);

if(ret)
{
switch(_fieldIdToCheck)
{
case fieldNum(TableName,Quantity):
if(this.Quantity>100)
{
          error("Quantity should be less than 100);
}
break;
//you can add as many conditions as per the number of fields.
//same condition can be shared by two fields ..see below eg.
case fieldNum(TableName,Price):
case fieldNum(TableName,PriceMST):
if(this.PurchQty == 0)
{
          error("Quantity cannot be zero");
}

}

similarly the event handler for the D365 is written as follow.
Post -> it gets called after the standard validatefield.
[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 > 100)
                {
                    ret = ret && checkFailed("Line per cent is Invalid");
                }
                break;
        }

        args.setReturnValue(ret);
    }

Thanks,
Vikas Mehta.

1 comment:

AZURE INTERVIEW QUESTIONS AND ANSWERS

AZURE INTERVIEW QUESTIONS AND ANSWERES 2021 2. What is cloud computing? Explanation:  It is the use of servers on the internet to “store...