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
Vishnu_SFDCVishnu_SFDC 

Error for a long text Field In javscript when trying to merge fields

Here is the code i am using.

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}


var c = new sforce.SObject("Lead");
c.id = "{!Lead.Id}";
c.Contact_Attempt__c = 'True';

var discription = prompt("Call Discription", "");
var notes = "{!Lead.Discussion_Notes__c}";
var note = notes +  "\r" + "{!User.Alias}" + "--" + discription + "--" + "{!NOW()}" ;
c.Discussion_Notes__c = note;
result = sforce.connection.update([c]);

window.location.reload();

Discussion_notes__c is the long text area i am using.. Its working for 1st two times. After it creates two lines it gives me a error saying "A problem with the OnClick JavaScript for this button or link was encountered:unterminated string literal"
Best Answer chosen by Vishnu_SFDC
Marty C.Marty C.

Hello, Vishnu,

I believe you can fix your problem by using the JSINHTMLENCODE() function[1]. See the modified line below when instantiating notes in your code.

var notes = "{!JSINHTMLENCODE(Lead.Discussion_Notes__c)}";

The problem is that every time you add new notes, a line break is added to the text stored inside Lead.Discussion_Notes__c. When you bring this value back into JavaScript as-is, it ends up writing the line break to your JavaScript inside your string declaration, which is not allowed. Salesforce's JSINHTMLENCODE() function magically handles the line breaks so that they don't break your code and still allows the line breaks be retained when going back into the database.

All Answers

Marty C.Marty C.

Hello, Vishnu,

I believe you can fix your problem by using the JSINHTMLENCODE() function[1]. See the modified line below when instantiating notes in your code.

var notes = "{!JSINHTMLENCODE(Lead.Discussion_Notes__c)}";

The problem is that every time you add new notes, a line break is added to the text stored inside Lead.Discussion_Notes__c. When you bring this value back into JavaScript as-is, it ends up writing the line break to your JavaScript inside your string declaration, which is not allowed. Salesforce's JSINHTMLENCODE() function magically handles the line breaks so that they don't break your code and still allows the line breaks be retained when going back into the database.

This was selected as the best answer
Marty C.Marty C.

Sorry, I left the link out in my previous reply!

[1]: JSINHTMLENCODE() function (http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_functions.htm)

Ravi Jampana 14Ravi Jampana 14
That's Awesome Marty , good explaination on JSINHTMLENCODE() function .. We have been using the same Javascript  cutome button from last two years ,never faced this issue and worried about myslef why the hack its throwing the exception now ..reaseon found first time user entered mutiple lines in targted field which is Long Text Area data type.