Solved

how to escape double quotes

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now