I recently ran into an interesting issue with using the FieldObject() method on the DictTable class when programming in X++ for D365FO.
The scenario to replicate the issue is to create a DictTable object and then perform a fieldObject() method call on a field that doesn’t exist.
According to Microsoft documentation, this method should return null if the object could not be created.
If we look at the scenario below we can see that if you instantiate the object via the constructor this process works as expected (scenario #1 below):
Note: In the example below I am using the CustTable (table ID 10347), the AccountNum field (field ID 1), and a fictitious field to intentionally cause the object creation to fail (field ID 219, the CustTable has 218 fields out of the box).
But if you, create a DictTable object first and then use the fieldObject() method the null check succeeds (which is incorrect) but if you try and access this object in any way you get the following error (DictField object not initialized’:
I wanted to give everyone a heads up if you are trying to obtain a DictField object be sure to use the object constructor and not the fieldObject() method on the DictTable class as this can lead to issues.