Tuesday, March 15, 2016

Unable to cast object of type 'Dynamics.Ax.Application.DictField' to type 'Dynamics.Ax.Application.SysDictField

I found some standard Microsoft code in AX which was working fine in x++ however, when the code executed in IL (Aif Interface), it would throw an error

"Unable to cast object of type 'Dynamics.Ax.Application.DictField' to type 'Dynamics.Ax.Application.SysDictField"

I found that the variable was declared as SysDictField but the object was instantiated as new DictField() - this works fine in x++ but would fail in IL based on OOP principles. 

The solution was to change the following line of code from new dictField(...) to new sysDictField(...)

SysDictField        dictField;

dictField   = new dictField(_variable.RefTableId,_variable.RefFieldId);

should be:

dictField   = new SysDictField(_variable.RefTableId,_variable.RefFieldId);