Wednesday, June 24, 2015

Dynamics GP - watch out for using "CHANGE" when you really meant tot use "CHG"

Consider the code:

in string RecordID;
inout table RecordTable;
optional in integer Op = GET;
assert Op = GET or Op = CHG;
{the rest goes on to get or change the table}

What happens if the caller passes in CHANGE? It'll do a get. Why? Because the constants GET = CHANGE. The assert attempts to protect against this mix up, but can't, because when you pass in CHANGE Op=GET is true, and therefore the assert succeeds.

If you accidentally use CHANGE instead of CHG, you'll run into runtime issues. A workaround to get the compiler to catch this would be to use string constants instead of integers.

For example, if I added the constants sGET="GET" and sCHG="CHG", I could then change the code to the following:
in string RecordID;
inout table RecordTable;
optional in string Op = sGET;
assert Op = sGET or Op = sCHG;
{the rest goes on to get or change the table}

this would prevent the problem of passing in "CHANGE", because if you tried to pass in CHANGE you'd get a compiler error since CHANGE is an integer constant and it's expecting a string. 

No comments:

Post a Comment

There was an error in this gadget