+ Start a Discussion
Glenn WeinsteinGlenn Weinstein 

Javascript syntax error when using numeric merge fields

I'm posting this problem & solution to help others who may encounter a similar issue.

In s-controls, Salesforce.com performs substitutions on merge fields (e.g. {!Account_Name}) before sending the Javascript to the client's browser for processing.  This means merge fields can't be used in the same way as normal Javascript variables.  This doesn't usually present a problem, but special caution is required when working with a numeric merge field (as opposed to a string merge field).  If the field can be null, you could cause a sneaky syntax error in your Javascript.  For example, consider the following line of Javascript in an s-control:

if ({!Contact_Age} > 65) { doSomething(); }

This looks innocent enough, but if Contact_Age is null, the line will be passed to the browser as:

if ( > 65) { doSomething(); }

Most browsers (certainly Firefox and IE) will throw a syntax error and refuse to process the Javascript, even if that specific line of code is never executed - Javascript interpreters tend to scan for syntax errors prior to starting runtime interpretation.  So - how can we work around this?  The easiest way is to ensure any inline use of a numeric merge field is wrapped in a string, then converted back to a number as follows:

if (Number("{!Contact_Age}") > 65) { doSomething(); }

The Number() function will evaluate a blank string ("") as the number 0, so be sure that your logic works correctly by treating a numeric value as 0 when it's null.  If this causes a logic problem, you can always test for null first, with a line of code like this:

if "{!Contact_Age}" == "" { assumeAgeIsNull() };

I hope this helps someone!

Glenn Weinstein
Principal
Appirio, Inc.



michaelforcemichaelforce

I'm still getting used to the relaxed approach to data types in javascript.... so there may be nuances I am missing here... but my initial reaction is to suggest that you store the merge field in a variable immediately, then use that variable to do any comparisons.

Code:

var c_age = {!Contact_Age};

if(c_age=="null")
    c_age=0;


 That may do the trick, no?

GlennAtAppirioGlennAtAppirio
Michael,

Unfortunately, your code won't work - it demonstrates the problem I was describing.  If the "age" field is in fact null, once the Salesforce.com merge field subtitution is done, the Javascript that will be passed to the client's browser will be:
var c_age = ;

if(c_age=="null")
c_age=0;

That will result in a syntax error in the browser's Javascript interpreter, pointing to the "var c_age = ;" line.  (If you're still not convinced, go ahead and try this in an s-control.  I use Firefox, because the error console (Tools-->Error Console) is so handy to view.)

I agree that Javascript's "relaxed approach" to datatypes is frustrating in some ways.  However, it's not really the issue here - even if Javascript were a strongly typed language, the field substitution would have the same issue.

Message Edited by GlennAtAppirio on 01-08-200709:45 PM

michaelforcemichaelforce

Point taken... I have not worked a lot with merge fields, so I appreciate the lesson.

ok, here is attempt number two...

You could create a formula field in salesforce which will be equal to contact_age if there is a value in it, and if not, then it will be equal to zero.  Then reference that field instead contact_age in your s-control.

GlennAtAppirioGlennAtAppirio
That would work, but all things considered, it's probably more straightforward and less invasive to use the approach I originally suggested in this thread.  :)
michaelforcemichaelforce

Haha... good point... I got carried away looking for another way to skin the cat.  But c'mon, I have to save face after my first idea flopped.  :smileywink:

Great post... and thanks for letting me think out loud!