I'm working on an inline visualforce page that is embedded into a standard lead detail record. I'm using javascript logic on the vf page to get the value of a field and then, depending on the value retrieved, display different messages. The issue I'm a having is that I'm not able to test in the javascript whether the field is null/blank/''/"".

My code looks like:

<div id="divFDNC"></div>

    <script language="javascript">

          var gIE = document.all ? true:false;
          if({!Lead.Checkbox__c} == true){

            if({!Lead.Custom_Status__c} == '00' || {!Lead.Custom_Status__c} == ''){
               document.getElementById('divFDNC').innerHTML = '<font style="font-size: 11px;" class="FDNC_Message">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>MY MESSAGE</b></font>'
               document.getElementById('divFDNC').innerHTML = '<font style="font-size: 11px;" class="FDNC_Message">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>MY MESSAGE 2</b></font>'

The message works when I enter '00' into the Custom_Status__c field. However, when the Custom_Status__c field is blank, then it doesn't display the message like it should. I've tried the following to get it to work (no luck on any of them):

'{!Lead.Custom_Status__c}' == '' / null / NaN / undefined

"{!Lead.Custom_Status__c}" == "" / null / NaN / undefined

{!Lead.Custom_Status__c} == '' / null / NaN / undefined

Does anyone know how to properly test that a field is blank, using javascript? Thanks in advance for any help you can provide.

  • In JS no need to use ==. Reserve that for Apex. Just test against "" for an empty string. – crmprogdev Sep 2 '15 at 17:57
  • 1
    The check with quotes around '{!Load.Custom_Status__c}' should have worked as it works for me. Alternatively you can change it to if{!(ISBLANK(Lead.Custom_Status__c)} without any quotes. – Aayush K Sep 2 '15 at 18:10
  • I think it would need to be if ({!ISBLANK(...)}) with parentheses. – Adrian Larson Sep 2 '15 at 18:12
  • Thank you all for the responses. Still not working...not sure what's going on. – rwegner7 Sep 2 '15 at 20:09

Anything that doesn't strictly evaluate to a native value should be enclosed in quotes, and you should also escape those values to avoid XSS injection attacks.

I would personally write the following code:

if({!OR(Lead.Custom_Status__c = '00', ISBLANK(Lead.Custom_Status__c))}) {

Salesforce will evaluate this to


when the status is blank or set to 00, and



Alternatively, if you want to use the value directly, make sure you use JSENCODE:

var customStatus = "{!JSENCODE(Lead.Custom_Status__c)}";
if(customStatus == "00" || !customStatus) {
  • I would agree with you (and I do like your alternative suggestion of using the var), but it goes beyond what you're mentioning as a solution. I'm not a js devloper, but I found that one of the reasons my js wasn't working was because I was giving the field different values in my code. When trying to get null, I originally had '{field}' = '' || {field} = '00'. I found that it only worked when all of the {field}'s had the "" around them --> "{field}" = "" || "{field}" = '00'. Only then did all values get checked – rwegner7 Sep 3 '15 at 19:02
  • @rwegner7 I recommend this technique doubly-so if JavaScript isn't your strong point. By having the system evaluate stuff to a native true/false value, you don't have to worry about how/if/where to quote, how quotes should work, etc. You can have salesforce.com evaluate the expression and give JS a simple Boolean to execute code from. I will admit, JS can be daunting, and so using formula expressions might be a more natural form to work with. – sfdcfox Sep 3 '15 at 19:13
  • I'm definitely a newbie at js, I think the var method you mentioned is the strongest way to do it, ensures the most consistency. Thanks for your suggestions! :-) – rwegner7 Sep 3 '15 at 19:35

Simple answer is that Salesforce is just weird when it comes to javascript. Even though the Lead.Custom_Status__c field is a text field, for some reason the javascript had different requirements when reading number values in the field vs alphabetical values.

Here's what worked:

if("{!Lead.Custom_Status__c}" == '00' || "{!Lead.Custom_Status__c}" == "" || "{!Lead.Custom_Status__c}" == "AA")

Notice how for the number values you only need single quotes around the value (''), but for text and null you need double quotes (""). I'm not sure why, but that's what solved it.

  • 1
    It may have worked, but you have no idea why it worked. This answer is pretty far off the mark. Both quotes have the same behavior in JavaScript. There's better ways to accomplish your goal. – sfdcfox Sep 3 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.