Uncaught SyntaxError: Unexpected identifier because of special characters and foreign language names present in Account

852    Asked by IsaacRoss in Web-development , Asked on Aug 1, 2021

 I have developed page with autoComplete feature. I'm getting an Uncaught SyntaxError: Unexpected identifier error because of symbols used in Account Names such as "诸城金盛-abc Commerce and" Co., Ltd. I'm posting my code for reference.

Visualforce page Code:

<meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Autocomplete - Default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <style> .ui-autocomplete { max-height: 100px; overflow-y: auto; overflow-x: hidden; } * html .ui-autocomplete { height: 100px; } td.data2Col { text-align: center !important; } table { border:0px; border-collapse:collapse; border-spacing:0px; } td,th { padding:0px; border-width:0px; margin:0px; } th{ width : 50px !important; } th{ width : 50px !important; padding-bottom: 20px; } table#mytable tr td:first-child{display:none;} table#mytable th{width:12%} table#mytable td{text-align:left;} </style> [removed][removed] [removed][removed]

[removed] $( function() { var availableTags = {!accList}; $('[id$=AccountIds]').autocomplete({ source: availableTags }); } ); [removed]

Apex Code:

public class SFI_AccountExtension { public Account accountRecord { get; set; } public List < String> accList { get; set; } public String selectedAccount{ get; set; } public SFI_AccountExtension(ApexPages.StandardController sc) { selectedAccount = ''; accountRecord = (Account) sc.getRecord(); getAccountList();//method to load list of accounts } //Method to supply list of account with the record type name 'SAP Sold-To Customer' present in salesforce for auto complete public List < String> getAccountList() { accList = new List < String> (); Id RecordTypeId = [SELECT Id, Name FROM RecordType WHERE SobjectType = 'Account' AND Name = 'SAP Sold-To Customer' limit 1 ].id; for (Account acc: [select name, SAP_Customer_Number__c from Account where recordTypeId =: RecordTypeId]) { accList.add('"' + acc.SAP_Customer_Number__c + ' - ' + acc.name + '"'); } return accList; } }

Why getting syntaxerror: unexpected identifier? Can somebody please tell how to avoid this type of error.

Answered by Harry Butler

Uncaught SyntaxError: Unexpected identifier. One of the most common reasons is that you're trying to mutate (change) a const variable.


It sounds like you'll need to encode your data:

        var available tags = JSON.parse("{!JSENCODE(accList)}");

This may still result in errors, but it should be closer to what you're looking for.



Your Answer

Interviews

Parent Categories