Activity Forums Salesforce® Discussions “Unexpected token ‘{ “ error on creating a Javascript button on Opportunity

  • “Unexpected token ‘{ “ error on creating a Javascript button on Opportunity

    Posted by Karen on March 17, 2016 at 10:03 am

    I want to create a JS button on the Opportunity to replace the current ‘edit’ button. The button would check if some of the feilds have a value, and if so direct the user to a visualforce page, and if not then direct them to the normal edit behaviour.
    After saving the button, I get a dialog error “unexpected token '{'”. What am i missing? The code i am using is -

    IF('{!Account.Type}' == "New") && '{!Opportunity.Ad__c}' == null{
    window.parent.location.href = "/apex/OpportunityEditOverride?id="+'{!Opportunity.Id}';
    }
    else{
    window.parent.location.href = "/"+'{!Opportunity.Id}'+"/e?retURL=%2F"+'{!Opportunity.Id}';
    }

    shariq replied 6 years, 2 months ago 5 Members · 4 Replies
  • 4 Replies
  • Prafull

    Member
    March 22, 2016 at 9:58 am

    Hi Karen,

    Your ")" is not in correct place in your first line of code. This should be as below:-

    IF(‘{!Account.Type}’ == “New” && ‘{!Opportunity.Ad__c}’ == null){

  • SFDCHacker

    Member
    March 22, 2016 at 11:53 am

    If you are using the exact code you have typed earlier, then unfortunately you have made a small mistake.
    You are using CAPITAL “IF”.
    JavaScript is a case sensitive language and will not recognize ‘IF’ only ‘if’
    Also it seems like you have closed your if statement a bit early. So here is the updated code
    if('{!Account.Type}' == "New" && '{!Opportunity.Ad__c}' == null){
    window.parent.location.href = "/apex/OpportunityEditOverride?id="+'{!Opportunity.Id}';
    }
    else{
    window.parent.location.href = "/"+'{!Opportunity.Id}'+"/e?retURL=%2F"+'{!Opportunity.Id}';
    }

    Also in the last line, though it is not wrong, it is not really necessay to concatenate your URLs. The following line will also work fine
    window.parent.location.href = "/apex/OpportunityEditOverride?id={!Opportunity.Id}";

  • Parul

    Member
    September 19, 2018 at 9:24 pm

    Hi,

    To add suggestion: Javascript is case sensitive.. So make sure the keywords you have used in the logic.

    Thanks.

  • shariq

    Member
    September 19, 2018 at 10:19 pm

    Hi,

    By veiwing your code I found haven't close the brackets correctly in -

    IF(‘{!Account.Type}’ == “New”) && ‘{!Opportunity.Ad__c}’ == null{

    Close the bracket at the end of null.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs