[SalesForce] Unexpected Identifier for Onclick Javascript button

I created a custom button for custom object to place in the related list. The custom object has two look-ups, one for case and one for account. This button, when clicked, should open in a new window to save a record of the custom object, with the default values for each look-up to populate automatically depending on the record where the button was clicked. It works for every record so far, except one case. It then throws the error: A problem with the Onclick Javascript for this button or link was encountered: Unexpected identifier.

I couldn't find any differences between records where it works and doesn't.

Code:

window.open('a2U/e?CF00N600000037HI4={!Account.Name}&CF00N600000037HI4_lkid={!Account.Id}&retURL=%2F{!Account.Id}&CF00N600000037HI5={!Case.CaseNumber}&CF00N600000037HI5_lkid={!Case.Id}&retURL=%2F{!Case.Id}','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=yes,location=yes,status=yes');

Best Answer

Use JSENCODE, because your account name has an apostrophe (single quote), which causes the string to be malformed. It needs to be properly escaped for JavaScript to not parse it incorrectly.

window.open('a2U/e?CF00N600000037HI4={!JSENCODE(Account.Name)}&CF00N600000037HI4_lkid={!Account.Id}&retURL=%2F{!Account.Id}&CF00N600000037HI5={!Case.CaseNumber}&CF00N600000037HI5_lkid={!Case.Id}&retURL=%2F{!Case.Id}','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=yes,location=yes,status=yes');
Related Topic