1

I have this custom button that is working

var cw = window.open('/apex/CreateSample?scontrolCaching=1&id={!Servicio__c.Id}', 'Crear Muestra', "height=450,width=600"); 
(function detectUnload() { 
window.setTimeout(function(){ 
if (cw == null || cw.closed) { 
window.location.reload(); 
return; 
} 
detectUnload(); 
}, 100); 
})();

But I what only to do that if a State equals Complete. So I wrote this

if ({!Servicio__c.State__c}== "Complete") 
{
(var cw = window.open('/apex/CreateSample?scontrolCaching=1&id={!Servicio__c.Id}', 'Crear Muestra', "height=450,width=600"); 
(function detectUnload() { 
window.setTimeout(function(){ 
if (cw == null || cw.closed) { 
window.location.reload(); 
return; 
} 
detectUnload(); 
}, 100); 
})();)

}

else 
{
alert("You can not do this");
}

Does anyone now why is not working? The error is "unexpected identifier" Thanks you in advance!

2 Answers 2

2

You're using a "bare" merge statement. this results in an unquoted string literal in the middle of your code, resulting in an exception during compilation.

You can either quote the merge variable, or even better, you can resolve the statement to the literal true or false value. To accomplish that, perform the comparison within the merge statement:

if ({!Servicio__c.State__c='Complete'}) { 

In the browser, this will render as:

if(true) {

or

if(false) {

Bonus:

In JavaScript, you don't generally need to compare to null. A null value is "falsy", while a non-null value is "truthy".

if( !cw || cw.closed) {
2
  • Thank you for your answer! But it is still not working. I changed for: if ({TEXT(!Servicio__c.State__c)='Complete'}) { (var cw = window.open('/apex/CreateSample?scontrolCaching=1&id={!Servicio__c.Id}', 'Crear Muestra', "height=450,width=600"); (function detectUnload() { window.setTimeout(function(){ if (cw == null || cw.closed) { window.location.reload(); return; } detectUnload(); }, 100); })();) } else { alert("This lead can not be marked as paid."); }
    – Elena
    Nov 1, 2014 at 0:51
  • @Elena The ! has to immediately follow the opening { for it to count as a merge field. Also, you can use ISPICKVAL here, if you prefer: {!ISPICKVAL(Servicio__c.State,'Complete')}.
    – sfdcfox
    Nov 1, 2014 at 0:58
1

The "unexpected identifier" error is caused by the way Salesforce inserts its data into the javascript. In your case you need to add a quotes around the field you are inserting.

In your code:

if ({!Servicio__c.State__c}== "Complete")

Needs to become:

if ("{!Servicio__c.State__c}"== "Complete")

This is because when Salesforce loads your object's detail page, it fills in the data between {} with raw code, not strings. Your original code would look like this once the page is loaded: if(Complete=="Complete") when you intended it to be if("Complete"=="Complete")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .