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
aruna11.3081223889641084E12aruna11.3081223889641084E12 

Can we replace just a character "(double quote) in apex?

Hi,

 

I have fetched HtmlValue from EmailTemplate. This is returned as String. This String contains all HTML code so, contains many (")characters. I need to replace (") character from this string with (\") so that i should return this string in javascript code on VF.

 

I tried with str.replaceAll('"','\"');

and str.replaceAll('\"','\\"');

 

 

'The source string is: Str= '<table height="400" width="600" cellpadding="5" border="0" cellspacing="5" >
<br/><tr height="50" valign="top" >
<br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r1" locked="0" aEditID="c1r1" >
<br/>Dear {!Contact.LastName}</td>
<br/></tr>
<br/><tr height="300" valign="top" >
<br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r2" locked="0" aEditID="c1r2" >
<br/><P>Your conact id is: {!Contact.Id}</P></td>
<br/></tr>
<br/><tr height="50" valign="top" >
<br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r3" locked="0" aEditID="c1r3" >
<br/><P>Thanks,</P>
<br/><P>SFDC Dev Team</P></td>
<br/></tr>
<br/></table>;

Dont know if this is the problem with escape sequences?

Your help is appreciated...

Best Answer chosen by Admin (Salesforce Developers) 
aruna11.3081223889641084E12aruna11.3081223889641084E12

Hi all,

 

here i discovered the solution nfor it.

 

Whenever we query EmailTemplate object in Apex class to extract the html body of template, it gives us the complete string including new line characters.

 

Just replace the new line characters with ''.

If we want to use this String of Html body on Visualforce page, we cannot get this string successfully on VF because it still considers new line characters though these newline chars are removed from a string.

To display a HTML body in  javascript or VF, we need to form a sincle line String value.

 

So, i have followed the below steps to get a sincle line value of the Html body.:

 

sHtmlValue = sHtmlValue.replace('\"','\\"');

sHtmlValue = sHtmlValue.replace('\n','<br/>');

 

List<String> emailBodyParts = sHtmlValue.split('<br/>');

 

for(String s : emailBodyParts){

sHtmlBody = sHtmlBody+s.trim();

}

 

now sHtmlBody gives us a single line value on Vf and is rendered successfully on VF.

 

Observation:

Here is most important point to be noted that even we replace newlinewith '' or <br/> and debug the string in Apex,

complete body doesnt some in single line.

 

So, apply the above code and debug the line, it will come into single line, thus getting rendered on VF or javascript successfully.

All Answers

Ankit AroraAnkit Arora

Not sure but try using Replace instead of ReplaceAll .

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

WizradWizrad

String temp = '"Hello World"';

temp = temp.replaceAll('"', '\\"');

System.debug(temp.contains('\"')); //Outputs true

System.debug(temp.contains('\\"')); //Outputs false

Rahul SharmaRahul Sharma

Ankit is right, use .replace() instead of .replaceAll(),

See -

 

String temp = '"Hello World"';
temp = temp.replace('"', '\\"');
system.debug('====temp===='+temp);
----------------------------------
Result:
DEBUG|====temp====\"Hello World\"

 

aruna11.3081223889641084E12aruna11.3081223889641084E12

Yeah,

 

Even replaceAll() method works for other characters.. dont know why it didnt work for (").

Is there any specific reason for this?

 

Yes,It works with replace() method.

 

i have replaced all " chars with \" considering it will help me in getting the resultant string on visualforce page.

 

I got the below String:

 

String str = '<table height=\"400\" width=\"600\" cellpadding=\"5\" border=\"0\" cellspacing=\"5\" >
<tr height=\"50\" valign=\"top\" >
<td style=\" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;\" tEditID=\"c1r1\" locked=\"0\" aEditID=\"c1r1\" >
Dear {!Contact.LastName}</td>
</tr>
<tr height=\"300\" valign=\"top\" >
<td style=\" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;\" tEditID=\"c1r2\" locked=\"0\" aEditID=\"c1r2\" >
<P>This email is sent to your email id: {!Contact.Email}</P>
<P>Your conact id is: {!Contact.Id}</P></td>
</tr>
<tr height=\"50\" valign=\"top\" >
<td style=\" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;\" tEditID=\"c1r3\" locked=\"0\" aEditID=\"c1r3\" >
<P>Thanks,</P>
<P>Team</P></td>
</tr>
</table>';

 

The above String is complete template's html value.

I want to display the above String in alert() method of javascript on VF as below:

<script>

var ss = "{!str}"; // this will call getStr() method from apex class.

alert(ss);

</script>

 

But i am getting javascript error on page: "Unterminated Stirng constant".

If i put above Stirng in single line, it works fine.

I have replaced \n also using replace('\n',''); in apex class, but nnot getting this String in single line...

 

please suggest a way so that i can convert complete htmlvale of emailtemplate into single line.

Then only i will be able to use it on VF in javascript.

 


 

 

Rahul SharmaRahul Sharma

by doing something like this will sole your problem,

str.replace('"','\\\\"');

 

I tried a simple example and getting proper alert, hope it help:

Controller:
public with sharing class StringManipulation
{
 public String str
 {	get;set;	}
 public StringManipulation()
 {
   str = '<table height="400" width="600" cellpadding="5" border="0" cellspacing="5" ><br/><tr height="50" valign="top" ><br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r1" locked="0" aEditID="c1r1" ><br/>Dear {!Contact.LastName}</td><br/></tr><br/><tr height="300" valign="top" ><br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r2" locked="0" aEditID="c1r2" ><br/><P>Your conact id is: {!Contact.Id}</P></td><br/></tr><br/><tr height="50" valign="top" ><br/><td style=" color:#000000; font-size:12pt; background-color:#CCCCFF; font-family:arial; bLabel:main; bEditID:r3st1;" tEditID="c1r3" locked="0" aEditID="c1r3" ><br/><P>Thanks,</P><br/><P>SFDC Dev Team</P></td><br/></tr><br/></table>';
  str = str.replace('"','\\\\"');
 }
}

Page:
<apex:page controller="StringManipulation">
 <script>
  var ss = "{!str}"; 
  alert(ss);
 </script>
</apex:page> 

 

aruna11.3081223889641084E12aruna11.3081223889641084E12

Yes Rahul,

 

Thanks for the suggestion, but below part was already working. 

I am not getting the reason, but this concept is now irritating... 

Whenever i take that HTMLValue of email template into literal String variable just as you assigned string literal to "str" variable, then we can retrieve that htmlvalue in alert on javascript. 

But if i do below operation and assign that value of HTMLVALUE variable to String variable, it gives me error "Undertermined String constant..." 

EmailTemplate emailTemp = [Select e.Subject, e.Name, e.IsActive, e.Id, e.HtmlValue, e.Body FromEmailTemplate e where e.name =: 'SampleCustomTemplate'limit 1];

 

String val1 = emailTemp.HtmlValue;

System.debug(val1);

it shows o/p as in 3 different lines.

 

line1: <table height=\"400\" width=\"550\" cellpadding=\"5\" border=\"0\" cellspacing=\"5\" ><tr height=\"400\" valign=\"top\" ><td style=\" color:#000000; font-size:12pt; background-color:#FFFFFF; font-family:arial; bLabel:main; bEditID:r3st1;\" tEditID=\"c1r1\" locked=\"0\" aEditID=\"c1r1\" ><P>hi {!Contact.Name}</P><P>&nbsp;</P>
line2: <P>Thanks for calling us.</P><P>&nbsp;</P><P>We will get back to you soon at: {!Contact.Email}</P><P>&nbsp;</P><P>Thanks,</P>
Line3: <P>Team</P></td></tr></table>

 

 

continued.....

aruna11.3081223889641084E12aruna11.3081223889641084E12

continued....

 

String originalValue =''+emailTemp.HtmlValue;

originalValue = originalValue.replace('\"','\\"');

originalValue = originalValue.replace('\n','<br/>');

List<String> emailBodyParts = originalValue.split('<br/>');

System.debug('emailBodyParts size ==> '+emailBodyParts.size());

This debug shown me value 3....

so i merged these three lines using simple string concatenation as below:

 

String finalStr = emailBodyParts[0]+emailBodyParts[1]+emailBodyParts[2];

 

if i return this finalStr to page, it should show me the alert.. but still am getting same error.. "Undertermined String constant."...

 

 

aruna11.3081223889641084E12aruna11.3081223889641084E12

continued.... Also, when i write debug statement for finalstr...

it is still showing me Complete string divided in three lines instead of 1 line... so i think it is still not merged in single line...

 

Please help.. if anyone knows solution for this.. or came accrross such scenario before...

 

David81David81

I believe what you'll need to so is replace any line breaks ('\n' or '\r\n') with '\\\\n'

 

If you'd like to use replaceAll for your double quotes, you can use replaceAll('\"','\\\\"')

aruna11.3081223889641084E12aruna11.3081223889641084E12

Hi all,

 

here i discovered the solution nfor it.

 

Whenever we query EmailTemplate object in Apex class to extract the html body of template, it gives us the complete string including new line characters.

 

Just replace the new line characters with ''.

If we want to use this String of Html body on Visualforce page, we cannot get this string successfully on VF because it still considers new line characters though these newline chars are removed from a string.

To display a HTML body in  javascript or VF, we need to form a sincle line String value.

 

So, i have followed the below steps to get a sincle line value of the Html body.:

 

sHtmlValue = sHtmlValue.replace('\"','\\"');

sHtmlValue = sHtmlValue.replace('\n','<br/>');

 

List<String> emailBodyParts = sHtmlValue.split('<br/>');

 

for(String s : emailBodyParts){

sHtmlBody = sHtmlBody+s.trim();

}

 

now sHtmlBody gives us a single line value on Vf and is rendered successfully on VF.

 

Observation:

Here is most important point to be noted that even we replace newlinewith '' or <br/> and debug the string in Apex,

complete body doesnt some in single line.

 

So, apply the above code and debug the line, it will come into single line, thus getting rendered on VF or javascript successfully.

This was selected as the best answer
Rahul SharmaRahul Sharma

ohh.. That was the cause..

Good to know that its solved.



aruna11.3081223889641084E12aruna11.3081223889641084E12

Yeah, this was the root cause and took a lot of attempt to get into this solution...