0

I am trying to created a Javascript button on the Opportunity to replace the edit button. I want to check if some fields have a value, and if so direct the user to a VF page, if not direct them to the normal edit behavior. The button saves fine, but I get a dialog error "unexpected token '{'". Is there something wrong with my Javascript? Thanks.

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}';
}
2
  • 1
    In JS doesnt IF need to be lowercase? Also it looks like you closed your IF after New.
    – Dan
    Commented Mar 2, 2016 at 22:22
  • 2
    Worth pasting such JavaScript into jshint.com to check it. And also check your browser's console to see any remaining syntax errors that stop the code working.
    – Keith C
    Commented Mar 2, 2016 at 22:40

2 Answers 2

7

You should be enclosing your conditions in the merge field; this makes it a lot easier to read:

if( {!ISPICKVAL(Account.Type, 'New') && ISNULL(Opportunity.Ad__c) } ) {

As noted in the comments, JavaScript is cAsE SeNsItIvE, so make sure you're using correct keywords (var, if, for instead of VAR, If, FoR).

Also, you don't need to do the string concatenation in JavaScript, because merge fields work in-place:

window.parent.location.href = "/apex/OpportunityEditOverride?id={!Opportunity.Id}";
1
  • 1
    For a lot of newbies at least, the merge syntax is probably best if you use its formula logical operators too. AND(condition1, condition2), etc. Nice answer though, +1.
    – Adrian Larson
    Commented Mar 2, 2016 at 22:42
3

Please do take note that Javascript is a case sensitive language, which means that every method or variables that you type on your code should match with what's in the JS library.

2.2. Javascript Case Sensitivity

I've also updated your code since it seems that you've enclosed your first if statement after your first condition.

    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}';
    }
3
  • 6
    +1, but the "problem' with this approach is that your code is subject to injection attacks. At minimum, by not using server-side substitutions, either Type or Ad__c might be injected in to. The only "safe" thing to merge are fields that are guaranteed to never be the wrong type: Id, Decimal, Date, DateTime, etc are all safe, while URL, Text, Phone, and so on should be encoded with JSENCODE.
    – sfdcfox
    Commented Mar 2, 2016 at 22:40
  • 1
    Isn't there an issue when you mix double with single quotes? Also Sean, how is that condensed milk tube? Any left? :)
    – dzh
    Commented Mar 2, 2016 at 22:49
  • 1
    @sfdcfox didn't know about that. Wooop. learned something new today! Thanks! Cheers
    – sfdx
    Commented Mar 2, 2016 at 22:57

You must log in to answer this question.

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