+ Start a Discussion
sf consultant.ax380sf consultant.ax380 

Use server side SUBSTITUTE function to remove carriage returns and line feeds while within an s-control?

User enters data in text area on standard field (non visual force page)  return of save button has been overrriden to call an s-control. .S-control needs to populate the value of the merge field referencing that standard field  in a javascript var and append it to the url for an iframe. (Seems simple)

Darn CRLF's that are embedded in the Merge Field if user hits enter in the text area field cause disasterous results (unterminated string error) when applying the merge field value to a javascript var.

Why have I constantly been unsuccessful in using Substitute to remove the CRLF??  Has ANYONE gotten this to work?

I've read varous other hacks requiring additional presentation logic of hidden form fields  or hidden divs working but this is not a read only field. The user is entering this right before the s-control is called.  I've heard of maybe run an additonal query either using query or retrieve as opposed to evaling the merge field but this has not seemed to work for me either.

ie. .the below I am using to protect against single and double quotes but I've tried numerous deritives to project against carriage returns and Substitute just doesn't seem to want to do it.

var desc = encodeURI("{! Substitute(Substitute(Case.Description ,"\\","\\\\"),"\"","\\\"")}" );

Any help would be greatly appreciated as I've wasted numerous hours on this. (in any other scenario I would use the full service side power of java to remove unwanted stuff before applying to the javascript var but in the case of Force.com and using an s-control. from my understanding my server side capability in this context is somewhat limited.

Thank you!

Message Edited by sf consultant on 11-26-2008 07:56 AM

Message Edited by sf consultant on 11-26-2008 07:57 AM

Message Edited by sf consultant on 11-26-2008 07:58 AM

Can't you use javascript to replace?

var string = "{!SomeMergeField}";

var string2 = text.replace(/[\r\n]/g, "");

Message Edited by Harmpie on 11-26-2008 11:54 PM
sf consultant.ax380sf consultant.ax380
Hi there and thank you for your reply.

Unfortunately I don't think that will work..

The problem isn't so much parsing out the line feeds, the problem is the initial assignment of the merge field to the js value. (it will never get assigned because it causes a double line in the js in the browser.

After entering a value in the long text field such as
Case description now I will hit enter once
Case description now I will hit enter twice

End Desc
var caseDesc = "{!Case.Description}";
var newDesc = caseDesc.replace(/[\r\n]/g, "");


Error: unterminated string literal
Source File: https://cs2.salesforce.com/servlet/servlet.Integration—lid=01NR00000000GzD&ic=1
Line: 155, Column: 23
Source Code:
        var caseDesc = "Case description now I will hit enter once


Javascript does not (reliably) allow multiline literal strings.

Perhaps you could get the value from the DOM or from a URL instead? Then you could use the javascript replacement technique.
I understand the problem now. It's the javascript syntax error you're getting. Can't you filter out the crlf's in a formula field in SF, and merge this field?
sf consultant.ax380sf consultant.ax380
That's a good idea and I thought about that but .. but .. but.. formulas are not supported on fields of the type that I'm evaluating.. that being a large text field.. arghhhhhhh..  I'm going to go run into a brick wall now!! lol

Ok, last try then: What about setting up another (hidden) text field, which is used for the merge, which is populated by an APEX trigger which strips out unwanted content?
Let me illustrate my suggestion.

<div id="hiddenField" style="display:none">{!Case.Description}</div>
<script type="text/javascript">
var desc = document.getElementById('hiddenField').innerHTML.replace(/\s+/g, ' ');