Search code examples
formsvalidationfieldx++dynamics-365-operations

Validation with SysOperations on D365 X++


I'm new with x++ and D365, so I often get stuck in very simple tasks. In this case i have created a new field in the extension table EcoResProductParameter called MaxPercentage and put it into the EcoResProductParameter extension form, and I need to check if this value is bigger than the SalesPercentMarkup I select in the InventTable. If so, the percent is updated, else I will receive an error. I tried to do something like this in the DataContract class:

public boolean validate()
{
    boolean isValid;
    if(salesPercentMarkup <= parameterTable.MaxPercentage)
    {
        isValid = true;
    }
    else
    {
        isValid = checkFailed('The percentage has exceeded the max');
    }
            
    return isValid;
}

but it keeps returning the error because I don't know how to correctly access the value that the user manually insert into the MaxPercentage field, so the problem is in my if statement I suppose. Can anyone help me with this? Thank you


Solution

  • One way to do this would be to create Chain of Command extension class for the validateField method of the InventTable table.

    This allows you to add custom field validations. In the scenario described in the question, this could look like this:

    [ExtensionOf(tableStr(InventTable))]
    final class InventTable_Extension
    {
        public boolean validateField(FieldId _fieldIdToCheck)
        {
            // execute the standard validateField logic
            boolean ret = next validateField(_fieldIdToCheck);
    
            // add custom validation
            if (_fieldIdToCheck == fieldNum(InventTable, SalesPercentMarkup))
            {
                EcoResProductParameters ecoResProductParameters = EcoResProductParameters::find();
                if (this.SalesPercentMarkup > ecoResProductParameters.MaxPercentage)
                {
                    // note that the message should be a label and put in double quotes
                    ret = checkFailed('The percentage has exceeded the max'); 
                }
            } 
    
            return ret;
        }
    }