Search code examples
dateaxaptadynamics-ax-2012-r2

How can the ToDate be kept after the FromDate in a table record?


I have a table with two fields: FromDate and ToDate. I want to make sure that the ToDate value is always later than the FromDate value.

To do this, I want to set the ToDate value to the FromDate value + 1. To do this I have implemented the following code in the validateField method of the table:

boolean ret;

ret = super(_fieldIdToCheck);

if (ret)
{
    switch (_fieldIdToCheck)
    {
        case fieldNum(MyTable, FromDate):
            this.ToDate = this.FromDate + 1;
    }
}

return ret;

This implementation works well, but the value of ToDate can be changed to a value before FromDate. How can this be prevented?


Solution

  • As Alex said, use the table method modifiedField to do field dependant modifications.

    If you change the field by code remember to call the method yourself.

    Maybe you should make the assignment conditional to avoid overwriting user entered date. This will also work well if ToDate is initially null.

    public void modifiedField(FieldId _fieldId)
    {
        super(_fieldId);
        switch(_fieldId)
        {
            case fieldNum(MyTable, FromDate): 
            case fieldNum(MyTable, ToDate): 
                if (this.ToDate < this.FromDate) // mostly first time
                    this.ToDate = this.FromDate + 1;
                break; // don't forget the break
        }
    }