Friday, September 4, 2020

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.

 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);
    }

No comments:

Post a Comment