Solved

how to escape double quotes

Posted on 2009-05-13
4
834 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-
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 36
maps stopped work unsure why 7 34
jqury 17 28
tiny glitch in my main slider 3 19
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

776 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