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
Arjun SrivastavaArjun Srivastava 

unterminated string literal error in salesforce due to data in multiple lines

 

I am using a standard page on which there is a button in which JavaScript code is written. It is calling a Controller (Class).

When I ever used click on the button,It gives me error:

A problem with the OnClickJavaScriptforthis button or link was encountered:
unterminated
string literal.

Javascript:

try
{
  alert
('hi1');
}
catch(Err)
{
  alert
('Error  in creation'+Err);
}

After searching and done some hit and trial,used a simple alert code above.

I found that. whenever i used data where there is a new line space between them i.e. data in multiple times.This error encounters no matter if you are using that particular field in JavaScript code and class or not.

 

 

 

What i am trying to say is : i am not using that field anywhere in the JavaScript code. I am just simply using alert('hi');.

Nothing else you can also try just create a custom button on sfdc detail page ,call JavaScript on it and put data in any Long text area field with multiple line.You will get same error. Any work around for this?

bob_buzzardbob_buzzard

You'll need to escape the text area field to replace line breaks with a representation that javascript likes - '\n'.

Arjun SrivastavaArjun Srivastava
var test='{!Prospect__c.test__c}'.replace(/["']/g, '"').replace(/^\s+|\s+$/g,"").replace('\n',' ').replace('\r',' ');


alert(test);

 I am doing above thing (custom button on standard detail page and javacript code on it).but still no success.

 "A problem with the OnClick JavaScript for this button or link was encountered:
 unterminated string literal".

above test field(Long text area) on detail page contain data in multiple lines.

Any help for this?

thanks for the reply

 

surendra kumar 7surendra kumar 7
trigger duplicatename on student__c(before insert,before update)
{
list<student__c> stlist=trigger.new;
list<student__c> stnewlist=new list<student__c>();
for(student__c st=stlist)
{
stnewlist=[select id,name from student__C where name=st.name];
if(stnewlist.size()>0)
{
st.adderror('you can't insert/update duplicate records');
}
}
}

Error Error: Compile Error: line breaks not allowed in string literals at line 10 column -1 plzz give me reply