You need to sign in to do that
Don't have an account?
Glenn 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.
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.
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:
That may do the trick, no?
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:
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
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.
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!