Solved

how to escape double quotes

Posted on 2009-05-13
4
837 Views
Last Modified: 2012-05-06
I have some javascript code that I'm using within Salesforce (CRM system).  The javascript is invoked on a button click.  What is happening is if the description field that is passed in thru the URL contains double quotes, it throws an error showing that a semi-colon is expected.

How can I escape the double qoutes if they are included in the description so the error does not occur?

this is the line for the description getting passed in:

00N70000002MF8u={!Service_Booking__c.Description__c}

So, if that value contains double quotes, the error is thrown.  I just need to escape the double quotes.

Thanks for any help.
{! requireScript("/soap/ajax/12.0/connection.js") } 
function doit(){ 
var ul="&CF00N70000002MF8y={!Service_Booking__c.Name}&00N70000002MF90={!Service_Booking__c.Start__c}&00N70000002MF8v={!Service_Booking__c.End__c}&00N70000002MF8t={!Service_Booking__c.Delivery_Type__c}&00N70000002MF8s={!Service_Booking__c.Delivery_Sub_type__c}&CF00N70000002MF4e={!Service_Booking__c.Services_Engagement__c}&00N70000002MF8u={!Service_Booking__c.Description__c}&00N70000002MF8r={!Service_Booking__c.Billable_Days__c}&CF00N70000002MF8z={!Service_Booking__c.Site__c}"; 
 
var newURL = "{!URLFOR( $Action.Service_Delivery__c.New, Service_Delivery__c.Id,[retURL=$Request.retURL] ,true)}"+ ul; 
 
if({!Service_Booking__c.Active__c}==true){ 
window.parent.location.replace(newURL); 
} 
else if({!Service_Booking__c.Active__c}==false){ 
alert("You attempted to convert Service Booking {!Service_Booking__c.Name}, which is no longer active. This usually means the booking has already been converted to a delivery. Check the record and make sure."); 
window.parent.location.replace("/apex/ConvertSingle?id={!Service_Booking__c.Id}"); 
} 
 
 
} 
doit();

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Expert Comment

by:sam2912
ID: 24377187
try adding this line below to replace them
function doit(){ 
var ul="&CF00N70000002MF8y={!Service_Booking__c.Name}&00N70000002MF90={!Service_Booking__c.Start__c}&00N70000002MF8v={!Service_Booking__c.End__c}&00N70000002MF8t={!Service_Booking__c.Delivery_Type__c}&00N70000002MF8s={!Service_Booking__c.Delivery_Sub_type__c}&CF00N70000002MF4e={!Service_Booking__c.Services_Engagement__c}&00N70000002MF8u={!Service_Booking__c.Description__c}&00N70000002MF8r={!Service_Booking__c.Billable_Days__c}&CF00N70000002MF8z={!Service_Booking__c.Site__c}"; 
 
ul = ul.replace(/\"/g,'\\\"');

Open in new window

0
 

Author Comment

by:-Dman100-
ID: 24378898
Hi sam2912,

I tried adding the line:
ul = ul.replace(/\"/g,'\\\"');

I tested using a description with double quotes and still the error occurred.

Any thoughts?  As soon as I remove the double quotes, everything works fine?
0
 
LVL 14

Expert Comment

by:sam2912
ID: 24381513
Try this. I can't actually test it out because the values are dynamic.
var regexp = new RegExp("\"", "g");
var rep    = '\"'
 
var booking_name = escape('{!Service_Booking__c.Name}');
    booking_name = booking_name.replace(regexp, rep);
 
var booking_start= escape('{!Service_Booking__c.Start__c}');
    booking_start= booking_name.replace(regexp, rep);
 
var booking_end  = escape('{!Service_Booking__c.End__c}');
    booking_end  = booking_name.replace(regexp, rep);
 
var booking_delt = escape('{!Service_Booking__c.Delivery_Type__c}');
    booking_delt = booking_name.replace(regexp, rep);
 
var booking_delst= escape('{!Service_Booking__c.Delivery_Sub_type__c}');
    booking_delst= booking_name.replace(regexp, rep);
 
var booking_se   = escape('{!Service_Booking__c.Services_Engagement__c}');
    booking_se   = booking_name.replace(regexp, rep);
 
var booking_desc = escape('{!Service_Booking__c.Description__c}');
    booking_desc = booking_name.replace(regexp, rep);
 
var booking_bd   = escape('{!Service_Booking__c.Billable_Days__c}');
    booking_bd   = booking_name.replace(regexp, rep);
 
var booking_site = escape('{!Service_Booking__c.Site__c}');
    booking_site = booking_name.replace(regexp, rep);
 
var ul= 
 
"&CF00N70000002MF8y="+booking_name+"&00N70000002MF90="+booking_start+"&00N70000002MF8v="+booking_end+"&00N70000002MF8t="+booking_delt+"&00N70000002MF8s="+b
 
ooking_delst+"&CF00N70000002MF4e="+booking_se+"&00N70000002MF8u="+booking_desc+"&00N70000002MF8r="+booking_bd+"&CF00N70000002MF8z="+booking_site+""; 
 
alert("You should see double quotes below if there are any:\n\n"+ul);

Open in new window

0
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 total points
ID: 24381521
Ignore the previous post. EE put extra line breaks in the long line.
<script type="text/javascript">
var regexp = new RegExp("\"", "g");
var rep    = '\"'
 
var booking_name = escape('{!Service_Booking__c.Name}');
    booking_name = booking_name.replace(regexp, rep);
 
var booking_start= escape('{!Service_Booking__c.Start__c}');
    booking_start= booking_name.replace(regexp, rep);
 
var booking_end  = escape('{!Service_Booking__c.End__c}');
    booking_end  = booking_name.replace(regexp, rep);
 
var booking_delt = escape('{!Service_Booking__c.Delivery_Type__c}');
    booking_delt = booking_name.replace(regexp, rep);
 
var booking_delst= escape('{!Service_Booking__c.Delivery_Sub_type__c}');
    booking_delst= booking_name.replace(regexp, rep);
 
var booking_se   = escape('{!Service_Booking__c.Services_Engagement__c}');
    booking_se   = booking_name.replace(regexp, rep);
 
var booking_desc = escape('{!Service_Booking__c.Description__c}');
    booking_desc = booking_name.replace(regexp, rep);
 
var booking_bd   = escape('{!Service_Booking__c.Billable_Days__c}');
    booking_bd   = booking_name.replace(regexp, rep);
 
var booking_site = escape('{!Service_Booking__c.Site__c}');
    booking_site = booking_name.replace(regexp, rep);
 
var ul= "&CF00N70000002MF8y="+booking_name+"&00N70000002MF90="+booking_start+"&00N70000002MF8v="+booking_end+"&00N70000002MF8t="+booking_delt+"&00N70000002MF8s="+booking_delst+"&CF00N70000002MF4e="+booking_se+"&00N70000002MF8u="+booking_desc+"&00N70000002MF8r="+booking_bd+"&CF00N70000002MF8z="+booking_site+""; 
 
alert("You should see double quotes below if there are any:\n\n"+ul);
</script>

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question