Quick Tip: Creating Dexterity System Variables

David Meego - Click for blog homepageHi everyone.

This is another Quick Tip post for Dexterity developers. Vaidy's recent post: "Illegal Address Field" Error while accessing a Global, raised the issue and so I thought I would respond with an explanation.

The issue is when you create a System Variable (global variable) from an existing Dynamics Global Field.  While you are in test mode, the code will work fine, but once a chunk is created you will get the following error:

Unhandled script exception: Illegal address for field '<Field Name>' in script <Script Name>. Script terminated.

The reason for this is easy to explain, once you understand how Dexterity handles the System Variable resources. System Variables are stored in application memory and can be referred with the "of globals" qualification.

System Variables are not actually stored as separate resources in the Dexterity dictionary. They are a subset of the Global Field resources, with a flag to mark them as System Variables. So the act of adding a System Variable does not create a new resource, but just edits the existing resource.

The scripts that use this newly added System Variable will compile and work fine when running in Dexterity Test Mode. However, once a chunk of the code has been created and installed into Runtime mode, you will get errors whenever the System Variable is referenced because it does not exist in the runtime environment.

Because we updated an existing variable with a Resource ID less than 22,000, it is not transferred into our addon dictionary during the extract stage of the chunking process. So in Runtime mode, the original field in the DYNAMICS.DIC is being used and this field is not marked as a System Variable.

The solution is to always create your own Global Field resource and then add it as a System Variable when you need a global variable. I always prefix its name with my company identifier ("MBS") and include a description of the field's purpose in the name. Now the will be a new resource with an ID greater than 22,000 which will be extracted when my chunk is created.

Note: Other common reason for the above error message are discussed in the following posts: Dex - Illegal address for field 'PowerUser' exception and Quick Tip: Solving Illegal Address Errors on Alternate Windows after installing a Service Pack.

Hope this explains what happened. 

David