1

I have written simple logic to through javascript alert if the description field on account object has some value I am displaying alert and if it null displaying different alert. But the below code throws error "Unexpected Token Else"

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var testField='{!Account.Description}';
var checking=sforce.connection.query("select Description from account where id='001p000000AgZk7' limit 1 ");
var recs = checking.getArray("records");
for(var i=0;i<recs.length;i++){
if(recs.length < 0)
alert('empty');
}
else{
alert('filled');
}
  • You have not closed the For loop, always look for JavaScript errors in dev tools. – Raul May 9 '17 at 13:32
  • even though i closed facing same error – saritha sari May 9 '17 at 13:34
1

According to your requirement, you do not need to query the description attribute.

You can get the description from merge field and better to compare with that value.

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var testField="{!Account.Description}";

if(testField !=null && testField.trim().length>0)
{
    alert('filled');
}
else
{
    alert('empty');
}

It has been assumed the custom button is on record detail page.

4

You missed the opening bracket around your if statement:

for (...) {
    if (...)
         //  ^ open bracket missing
    }
    else {
    }

That means the if block is simply the next statement, and your closing bracket after it terminates the for loop, making your code approximately:

for (...) {
}
else {
}

Which is obviously not valid syntax.

Syntax aside, your actual conditional is impossible to fulfill. The length of an array will never be less than zero. It is either empty (length=0), or populated (length>0). The value is strictly non-negative.

  • for(var i=0;i<recs.length;i++){ if(recs.length < 0){ alert('empty'); } } else{ alert('filled'); } done but no luck – saritha sari May 9 '17 at 13:36
0

I believe you do not require the for loop at all, because you are fetching only one record (using limit 1). So below code should work fine:

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var testField='{!Account.Description}';
var checking=sforce.connection.query("select Description from account where id='001p000000AgZk7' limit 1 ");
var recs = checking.getArray("records");

if(recs.length > 0)
  alert('filled');
else
  alert('empty');

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.