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
Dale WorthingtonDale Worthington 

Trying to remove illegal characters in JS so i can make a new record

Hi All

I have a JS button on a custom object that copies fields from the record an makes a new record in a different object. One of the fields im trying to copy is a long text area but it often has illegal char's and a lot of weird stuff in it.
 
ito.incidentDescription__c = "{!ncident__c.incidentDescription__c }";
Always results in an Illegal error

So im trying to clean it up an put it into an error box so i can confirm that it works but i still get illegal errors. The code im trying that still errors is:
 
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}

var incidentdesc = "{!Incident__c.incidentDescription__c}"
clean = incidentdesc.replace(/[|&;$%@"<>()+,]/g, "");

alert( 
clean
);

Can anyone offer up any possiable solutions to this?

Cheers
Dale
Best Answer chosen by Dale Worthington
Suraj GharatSuraj Gharat
Hi Dale,

There might be some characters (For instance double quote) in your "incidentDescription__c" field's content that possibly break your Javascript. You need to escape such charcaters first. To do so, you may "JSENCODE" function as shown below. 
var incidentdesc = "{!JSENCODE(Incident__c.incidentDescription__c)}";

All Answers

Gyanender SinghGyanender Singh
Hi Dale,

In place of this incidentdesc.replace(/[|&;$%@"<>()+,]/g, "");

Please copy and paste this code incidentdesc.replace(/[|&;$%@<>()+,]/g, "");

Thanks,
Gyanender Singh
07503868174
Suraj GharatSuraj Gharat
Hi Dale,

There might be some characters (For instance double quote) in your "incidentDescription__c" field's content that possibly break your Javascript. You need to escape such charcaters first. To do so, you may "JSENCODE" function as shown below. 
var incidentdesc = "{!JSENCODE(Incident__c.incidentDescription__c)}";
This was selected as the best answer
Dale WorthingtonDale Worthington
Hi Both,

Suraj's solution seemed more encompasing. I was able to output the text to a errorbox without any problem with this. However now i cannot insert it into another long text feild in another object.

You will notice in line 53 what i added based on the feedback from Suraj, when this line is here i get a success message but no record is actually written :/. When i remove it i get the expected behaviour.
 
{!REQUIRESCRIPT('/soap/ajax/27.0/connection.js')} 
getDate = function(dateObj){ 
var day = dateObj.getDay() < 9 ? '0'+dateObj.getDay() : dateObj.getDay(); 
var month = dateObj.getMonth() < 9 ? '0'+dateObj.getMonth() : dateObj.getMonth(); 

return dateObj.getFullYear()+'-'+month+'-'+day; 
} 

var thedatetime = function xsdDateTime(date) 
{ 
function pad(n) { 
var s = n.toString(); 
return s.length < 2 ? '0'+s : s; 
}; 

var yyyy = date.getFullYear(); 
var mm1 = pad(date.getMonth()+1); 
var dd = pad(date.getDate()); 
var hh = pad(date.getHours()); 
var mm2 = pad(date.getMinutes()); 
var ss = pad(date.getSeconds()); 

return yyyy +'-' +mm1 +'-' +dd +'T' +hh +':' +mm2 +':' +ss; 
} 
var billingworktype = "{!IncidentObject__Incident__c.Billing_Work_Type__c}";




if(billingworktype == "" || billingworktype == null) {
	
alert('The Billing Work Type is empty');
	
} 

else {

var ito = new sforce.SObject('Incident2Op__c'); 

ito.Name = '{!IncidentObject__Incident__c.Name}'; 
ito.OwnerId = '{!User.Id}'; 
ito.Note1__c = "{!IncidentObject__Incident__c.Invoice_Note__c}" ;
ito.WorkType__c = '{!IncidentObject__Incident__c.Billing_Work_Type__c}' ;
ito.Category__c = "{!IncidentObject__Incident__c.IncidentObject__FKCategory__c	}" ;
ito.LOE__c = "{!IncidentObject__Incident__c.Estimated_Level_of_Effort__c}" ;
ito.IncidentID__c = '{!IncidentObject__Incident__c.Name}' ;
ito.CurrencyIsoCode = '{!IncidentObject__Incident__c.CurrencyIsoCode}' ;
ito.Account__c = "{!IncidentObject__Incident__c.IncidentObject__FKAccountId__c}" ;
ito.Incident_Owner__c = '{!IncidentObject__Incident__c.IncidentObject__FKOpenById__c}' ;
ito.Price__c = "{!IncidentObject__Incident__c.Price__c}";
ito.Subject__c = "{!IncidentObject__Incident__c.Subject__c}" ;
ito.Combined_Total_Duration_Minutes__c = '{!IncidentObject__Incident__c.Combined_Total_Duration__c}' ;
ito.incidentDescription__c = "{!JSENCODE(IncidentObject__Incident__c.IncidentObject__incidentDescription__c)}" ;




var inote = new sforce.SObject('IncidentObject__IncidentHistory__c'); 
inote.Name = '{!IncidentObject__Incident__c.Name}' ;
inote.IncidentObject__FKAction__c = 'a0Y20000009ry5LEAQ' ;
inote.IncidentObject__description__c = 'Billing Opportunity Sent' ;
inote.IncidentObject__date__c =  thedatetime ;
inote.IncidentObject__FKUser__c = '{!User.Id}' ;
inote.IncidentObject__FKIncident__c = '{!IncidentObject__Incident__c.Id}' ;
inote.IncidentObject__duration__c = "00:15" ;



result = sforce.connection.create([ito]); 
result = sforce.connection.create([inote]); 

}

if(result[0].success == 'true'){ 
alert('An New ito - ' + ito.Name + ' was Created Successfully.'); 
} 

else{ 
alert( 
"An Error has Occurred.\r\n The Invoice Note, ELOE, and Billing Work Type must be populated.\r\n" + 
"Error: " + result[0].errors.message 
);
}