function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LButlerLButler 

JavaScript Error ILLEGAL TOKEN from textarea with line feed

I'm a relative newbie looking for some help.  Don't laugh, you were there once, too..  :(  

 

Here's the deal.  We have a custom link (button) on a standard account page.  The javascript is supposed to take the value of the custom physical_address__c field (textarea) and copy it to the standard BillingAddress fields (BillingStreet, BillingCity, etc).

 

Everything works when there is only one line in the custom field; but if the user has entered a line feed / carriage return (as many do), the script produces an ILLEGAL TOKEN error.

 

I've read everything I could find about stripping line feeds out using javascript, but so far no luck.  I suspect it's because I haven't figured out how to strip the characters before assigning the field value to a javascript variable.  Original code is below, for reference.

 

Any help out there?  I would really appreciate it..

 

Thanks,

 

Lewis 

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var a = new sforce.SObject("Account");
a.id = "{!Account.Id}";

a.BillingStreet = "{!Account.Physical_Address__c}";
a.BillingCity = "{!Account.Physical_City__c}";
a.BillingState = "{!Account.Physical_State__c}";
a.BillingPostalCode = "{!Account.Physical_Postal_Code__c}";
a.BillingCountry = "{!Account.Physical_Country__c}";
result = sforce.connection.update([a]);
window.location.reload();

mshelmanmshelman

What does the source look like when the page errors out? You should be able to View Source and see how the merge fields are being resolved, perhaps a first step to fixing the problem.

 

Mike

Richa KRicha K

Try using this... let me know if it works.....

 



{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var a = new sforce.SObject("Account");
var output = "{!Account.Physical_Address__c}";
for (var i = 0; i < input.length; i++) 
{
 if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) 
 {	
   i++;
   output += "<BR>";
 } 
 else 
 {
   output += input.charAt(i);
 }
}
a.id = "{!Account.Id}";
a.BillingStreet = output;
a.BillingCity = "{!Account.Physical_City__c}";
a.BillingState = "{!Account.Physical_State__c}";
a.BillingPostalCode = "{!Account.Physical_Postal_Code__c}";
a.BillingCountry = "{!Account.Physical_Country__c}";
result = sforce.connection.update([a]);
window.location.reload();

 

Karl NguyenKarl Nguyen

This is for those who are also encountering this issue.  There is a much easier way to fix this:

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var a = new sforce.SObject("Account");
a.id = "{!Account.Id}";

a.BillingStreet = "{!URLENCODE( Account.Physical_Address__c )}";
a.BillingCity = "{!Account.Physical_City__c}";
a.BillingState = "{!Account.Physical_State__c}";
a.BillingPostalCode = "{!Account.Physical_Postal_Code__c}";
a.BillingCountry = "{!Account.Physical_Country__c}";
result = sforce.connection.update([a]);
window.location.reload();

 

You may want to do the same approach with any fields you are using with funny characters.

Patrick Maxwell AppfolioPatrick Maxwell Appfolio
Did anyone ever find out an answer to this?  Karl's workaround does cause the error not to happen, but then we are left with URL encoded text that doesn't look very nice.