Wednesday, March 26, 2008

Hard-coded reformatting of Netherlands postal codes

My company discovered recently that Dynamics AX 4.0 is coded to reformat the Netherlands postal codes when setting the address field. Because this code is implemented on the AddressMap object, it affects anything that uses addresses (customers, vendors, sales orders, warehouses, etc.)

The postal codes for the Netherlands consist of 4 number and 2 letters, separated by a space. Many other countries also have a space in their postal codes, like Canada and Great Brittan.

Postal code City Country
2252 GN Voorschoten NL
V5K 1C6 Vancouver CA
E11 3RU London GB

For most postal codes in Dynamics AX, the way the it is entered in the database is the way it appears in the formatted address field on forms and reports. If you want a space in your postal code, then make sure it is entered that way in the AddressZipCode table. What I discovered, though, is that there is a hard-coded exception for the Netherlands.

When the FormatAddress or SetAddress methods of the AddressMap object are called, they check to see if the address is associated with ISO country code NL. If so, they insert a space with this instruction:

addressZipCodeId = substr (addressZipCodeId, 1,4) + ' ' 
+ substr(addressZipCodeId , 5,2);


Now this is fine if your Netherlands postal codes were entered without a space to begin with. The code above forces them to (what I can only guess is) the accepted format. This is a big problem, though, if your database already stores the accepted format for these postal codes; in which case you will end up with a mangled version with 2 spaces and missing the last character!

A quick fix is to add a line that checks to see if the Netherlands postal code is only 6 characters long and add the space only if it needs one.

if(strlen(addressZipCodeId) == 6)
addressZipCodeId = substr (addressZipCodeId, 1,4) + ' '
+ substr(addressZipCodeId , 5,2);