+ Start a Discussion
RurouniQRurouniQ 

Unexpected String error when passing JS variable to URL

This is probably an exceedingly simple oversight on my part, as I am new to JS and coding in general. So I apologize in advance! I have cobbled together this bit of JS from various sources in order to make a custom list button that replicates the "Send an Email" button in Salesforce, but does so with a few important differences:

  • Prepopulates the To and CC fields with text from other custom fields.
  • Auto-selects a specific email template.
  • Utilizes the merge fields of the email template, pulling data from a record in a custom object. This record would be selected via the checkboxes generated by the custom list button, like so:

The record

 

The user will check one item, then click the Email Comment button, which will pull in the record ID of the checked record and apply the data to the merge fields of the email template.

 

Now here's the problem. Whenever I do this now, I receive an error:

 

A problem with the OnClick JavaScript for this button or link was encountered:

Unexpected string

 

So now I'm stuck! Please help! Here is the code that is entered under the custom list button.

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} //adds the proper code for inclusion of AJAX toolkit 
var checked= {!GETRECORDIDS($ObjectType.Change_Comment__c)}; //grabs the records that the user is requesting to update 
var record = (checked.toString()); 

if (record[0] == null) { //if the button was clicked but there was no record selected 
alert("Please select one record."); //alert the user that they didn't make a selection 
} else if (record.length > 1) { 
alert("Please select ONLY one record."); 
} else { 
location.replace('/email/author/emailauthor.jsp?retURL=/{!SLX__Change_Control__c.Id}&p24={!SLX__Change_Control__c.Requestor_s_Email__c}&p4={!SLX__Change_Control__c.Reports_To_Email__c}&p3_lkid=' + record '&template_id=00X70000001SBNg&'); 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
*werewolf**werewolf*

Well you also forgot a + after the record and before the template_id string.

 

location.replace('/email/author/emailauthor.jsp?retURL=/{!SLX__Change_Control__c.Id}&p24={!SLX__Change_Control__c.Requestor_s_Email__c}&p4={!SLX__Change_Control__c.Reports_To_Email__c}&p3_lkid=' + record '&template_id=00X70000001SBNg&'); 

 

should read

 

location.replace('/email/author/emailauthor.jsp?retURL=/{!SLX__Change_Control__c.Id}&p24={!SLX__Change_Control__c.Requestor_s_Email__c}&p4={!SLX__Change_Control__c.Reports_To_Email__c}&p3_lkid=' + record + '&template_id=00X70000001SBNg&'); 

 

All Answers

*werewolf**werewolf*

The problem is likely that string that has "Tommy's Idea" in it.  The apostrophe in there is causing JS to think that your string has ended (remember that Salesforce.com inserts merge fields in verbatim), because you're using apostrophes to denote the start and end of strings.

 

You could change your location.replace method to have a string that stars and ends with double quotes instead of apostrophes.   That will also work in JS, although it will fail in exactly the same way if someone happens to put a comment in there that has a double quote in it.  There's no simple way I know of to get Salesforce.com to escape merge fields for you.

RurouniQRurouniQ

While this is almost certainly contributing to the problem (stupid me forgetting to account for that!), I've tested it with another comment that contains just one word and no punctuation, and I am still getting the same error. :smileysad:

*werewolf**werewolf*

Well you also forgot a + after the record and before the template_id string.

 

location.replace('/email/author/emailauthor.jsp?retURL=/{!SLX__Change_Control__c.Id}&p24={!SLX__Change_Control__c.Requestor_s_Email__c}&p4={!SLX__Change_Control__c.Reports_To_Email__c}&p3_lkid=' + record '&template_id=00X70000001SBNg&'); 

 

should read

 

location.replace('/email/author/emailauthor.jsp?retURL=/{!SLX__Change_Control__c.Id}&p24={!SLX__Change_Control__c.Requestor_s_Email__c}&p4={!SLX__Change_Control__c.Reports_To_Email__c}&p3_lkid=' + record + '&template_id=00X70000001SBNg&'); 

 

This was selected as the best answer
RurouniQRurouniQ

That did it! Thank you much!