-2

I am getting an "invalid or unexpected token" error in the javascript code below. This code is part of a Detail page button.

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

var dealObj = sforce.connection.query("SELECT Id, Name, Deal_Status__c, RecordType.Name FROM bDeal__c WHERE Id = '{! bDeal__c.Id }'"); 

var lstDeal = dealObj.getArray("records"); 

if (lstDeal.length == 0) { 
    alert('Deal not found'); 
} else { 
    if (lstDeal[0].Deal_Status__c == 'Active') { 
        var qr = sforce.connection.query("SELECT Id, Name FROM Early_Termination__c WHERE Deal_ID__c = '{! bDeal__c.Id }' "); 
        var lstET = qr.getArray("records"); 

        if (lstET.length == 0) { 
            var dealrt = lstDeal[0].RecordType.Name; 

            if (dealrt == 'Alliance (R/O)' || dealrt == 'Segment (R/O)' || dealrt == 'CBA (R/O)' || dealrt == 'Maestro/Cirrus (R/O)') { 
                var txt; 
                var r = confirm('{!$Label.ET_Prompt_Create_Record}'); 
                if (r == true) { 
                    var et = new sforce.SObject("Early_Termination__c"); 
                    et.Deal_ID__c = '{! bDeal__c.Id }'; 
                    et.Date_Created__c = new Date(); 
                    et.Status__c = 'In Development'; 
                    var dmlResult = sforce.connection.create([et]); 

                    if (dmlResult[0].getBoolean("success")) { 
                        window.location.href = dmlResult[0].id; 

                    } else { 
                        alert(dmlResult); 
                    } 
                } else { 

                } 
            } else { 
                alert('{!$Label.ET_Error_Not_Allow_Terminate}'); 
            } 
        } else { 
            window.location.href = lstET[0].Id; 
        } 
    } else { 
        alert('{!$Label.BD_Deal_Status_Not_Active}'); 
    } 
}

Could anyone please help me find the issue?

3
  • 2
    What's the error you're getting? (What unexpected token?) Oct 7, 2018 at 14:33
  • Hi.. Shane, error is "invalid or unexpected token"
    – JURY
    Oct 7, 2018 at 14:42
  • do you know what lines it's erroring on? If not, use console.log or alert statements to find out where your code makes it to.
    – dphil
    Oct 9, 2018 at 15:02

1 Answer 1

3

Given that your code doesn't have any obvious syntax errors, I suspect it's one of your Custom Labels havng a ' in it, causing the syntax error. Always encode your merge fields when they may have JavaScript special characters:

var r = confirm('{!JSENCODE($Label.ET_Prompt_Create_Record)}'); 

...

alert('{!JSENCODE($Label.ET_Error_Not_Allow_Terminate)}'); 

...

alert('{!JSENCODE($Label.BD_Deal_Status_Not_Active)}'); 

You may want to clean up the indentation and remove the empty else block. It'd also be possible to combine the two queries to make the code shorter. You could also reduce the complexity of the code by using return statements to reduce the indentation necessary.

3
  • Oh, wow -- you're right! The missing indentation made it look like there was an extra else block (besides the empty one). I'm going to edit the question to make it easier to read, and delete my answer! Oct 7, 2018 at 15:16
  • Thank You sfdcfox. You are correct. There is an issue with the custom labels. (ET_Prompt_Create_Record )var r = confirm('{!JSENCODE($Label.ET_Prompt_Create_Record)}'); is working fine. Thank You so much
    – JURY
    Oct 7, 2018 at 15:23
  • @Jury Always glad to help!
    – sfdcfox
    Oct 7, 2018 at 15:24

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