Troy's Blog

Life, family, work, and my experiences with products, services, stores, and the people I encounter each and every day.

My Photo
Name: Troy Pullis
Location: Minneapolis, Minnesota, United States

Friday, January 12, 2007

Naming a ColdFusion variable beginning with a number

The naming rules for creating ColdFusion variables are found here in the LiveDocs. The first bullet point is

  • A variable name must begin with a letter, underscore, or Unicode currency symbol.
However, I am doing some integration with SalesForce.com, and they have custom variables that are named like "00N30000000dqFd". Now, if I want to name a form variable with that name, and do some validation before posting the form, I was running into problems. Here is some sample code and comments that outline my struggles. If you comment out the 3 spots (lines 3, 8, and 13-15) that throw errors, the code will run and you will get the 3 cfdumps.

<cfset field = "00N30000000dqFd"> <!--- here is a variable that we assign a value starting with a number --->

<cfset 00N30000000dqFd = "test"> <!--- variable starting with number throws error: "00N30000000dqFd," on line 3, column 8, is not a valid identifer name. --->
<cfparam name="form.#field#" default=""> <!--- paraming "00N30000000dqFd" in the form scope is ok --->
<cfdump var="#form#" label="after param">


<cfset "form.#field#" = "new value"> <!--- doing this cfset method throws an error: The string "form.00N30000000dqFd" is not a valid ColdFusion variable name. --->
<cfset form["#field#"] = "1st value"> <!--- doing the structure method sets the variable ok --->
<cfdump var="#form#" label="after assigning">


<cfif IsDefined("form.#field#")> <!--- doing this IsDefined() method of checking if the variable exists in the Form scope throws an error: Parameter 1 of function IsDefined, which is now "form.00N30000000dqFd", must be a syntactically valid variable name.--->
<cfset "form.#field#" = "2nd value">
</cfif>
<cfif StructKeyExists(form,field)> <!--- doing this structure key check method is ok --->
<cfset form["#field#"] = "2nd value">
</cfif>
<cfdump var="#form#" label="end of demo">

CFDump output
after param - struct
00N30000000DQFD [empty string]

after assigning - struct
00N30000000DQFD 1st value

end of demo - struct
00N30000000DQFD 2nd value