+ Start a Discussion
Olof-ISAOlof-ISA 

Error when handling fields with Carriage Returns, Line Feeds and &-signs

Hi
I guess that this is a quite common and simple problem but I cant find any easy way out.
 
I'm trying to update an account with values from a description field on another object. Everything works just fin as loong as I dont use data that contains funny characters as "&", carriage returns and so on.  Tryign to update data with these values makes the script crash...
I'd be really greatfull if anyone have a solution to this!
 
Cheers!
 
/Olof
 
Script sample:
function UppdateAccount() {
var queryResult = sforceClient.Query("Select ID, Name, Description from Account where ID='" + s_AccID + "'");
if (queryResult.getClassName() == "SoapFault") {
alert(
"Error Updating Account: " + queryResult.toString());
}
else {
if (queryResult.size > 0)
{
if (queryResult.size > 0) {
var account = queryResult.records[0];
account.set("Description", s_ACText);
queryResult.records[0] = account;
var sr = sforceClient.Update(queryResult.records);
try {
if (sr.getClassName() == "SoapFault") {
alert(
"Error updating Account: " + sr.toString());
}
}
catch (e) {
if (sr[0].getClassName() == "SaveResult"){
return(true);
}
}
}
}
else
{
alert(
"Couldn't find account, not updated.");
}
}
}
vandervander
Is removing or replacing these characters an option?  If so you could scrub the description by calling a function like the following before making your update call:
 
Code:
function remove_chars(val)
{
 val.replace(/['&_,%`"@~#]/g, "");
return val; }

 
or
 
Code:
function replace_ampersand(val)
{
 val.replace(/&/g, "and");
return val; }

 
-Cory
darozdaroz
Which version of the AJAX toolkit are you using? I was under the impression that the needed elements should be escaped when sent thru the API calls....
DevAngelDevAngel
Judging by the syntax in your code snippet, you are using an older version of the toolkit.  The latest version is Beta 3.3 and is at https://www.salesforce.com/services/lib/ajax/beta3.3/sforceclient.js.

The B33 version uses CDATA sections during the serialization of object values and is not susceptable to special character issues.
Olof-ISAOlof-ISA

Thanks!!

The code doesn't work with B33 do can You easilly tell Why?

Cheers

/Olof

Olof-ISAOlof-ISA

Cheers!

Have You got any samples on how to do the escaping?

/Olof 

Olof-ISAOlof-ISA

Thanks a lot! That will do the trick as a quickfix while changing to B33.

/Olof