[SalesForce] Custom button – Java script error – missing ) after argument list

I have a created a custom button using Onclick Javascript action.

I have a custom picklist field on Opportunity; I am just checking the value selected for this field on click of this button.

If the value is X or Y or Z then it should stay in the same page with an alert message,else it should take them to a different site.

The below script works perfectly, but when I click this button on opportunities in which the opportunity name has double quotes then it throws the below error:

missing ) after argument list

e.g: " N "Third Avenue (Opportunity name)

Can anyone please assist me in trouble shooting and a workaround..

Javascript:

function checktype() {
if("{!Opportunity.Opportunity_Type__c}" == "X" ||
"{!Opportunity.Opportunity_Type__c}" == "Y" ||
"{!Opportunity.Opportunity_Type__c}" == "Z"
 )
{
alert("Opportunity type: {!Opportunity.Opportunity_Type__c} is not supported ");

return;

}
else
{
window.location.replace("https://Anothersite.COM");
}

}
checktype();

Best Answer

You can use the URLENCODE function on the oppotunity name when generating the url so that it retains all those special characters properly

window.location.replace("anothersite.com/login/…
{!Opportunity.Id}&SfdcOpName={!URLENCODE(Opportunity.Name)}&SfdcServerURL‌​=
{!API.Partner_Server_URL_280}&SfdcSessionID={!API.Session_ID}");

or you can use the javascript encodeURI function as well to get a proper url and redirect

Related Topic