I have a custom object named CPQ_Request_for_Proposal__c, which has a lookup(Text) value for Account__c.

I'm trying to code a button which will use the value of Account__c to get actual Account object. The first alert() comes up. However, my code keeps returning "Unexpected Identifier" before the second alert() appears. I'm new to using JavaScript in a Salesforce context. I'm sure I'm doing something simple wrong, but I haven't been able to figure it out after searching for an answer for a while. What am I doing wrong in my code?


// Visual hint to the user that the processing has started this.disabled = true; 
this.className = "btnDisabled";

var clientSite = new sforce.SObject("Client_Site__c");  
var rfpId = '{!CPQ_Request_for_Proposal__c.Id}';   
= '{!CPQ_Request_for_Proposal__c.Id}';

alert ( 'RFP Id = ' + rfpId );

var accountId = '';

var accountId = '{!CPQ_Request_for_Proposal__c.Account__c}';

alert ( 'accountId = ' + accountId );

When you code merge fields into JavaScript, you should use JSENCODE to avoid quotes and apostrophes from breaking your code. It works like this:

var x = "{!JSENCODE(Field)}";

It would seem to me that you're getting the name of the account, and that name contains a quoting character, such as "Bob's Hardware and BBQ."


The answer was to use the Select Field Type and Insert Field pickers. That yielded the following syntax, which works:

var accountId = '{!CPQ_Request_for_Proposal__c.AccountId__c}';

This is interesting (to me, at least) in that there is no AccountId__c field on the CPQ_Request_for_Proposal__c object.

  • Yes AccountId__c can only be found in the dropdown that contains merge fields..while creating a custom button. Apr 20 '15 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.