javascript - Unexpected Token Illegal with onclick Java Script in Salesforce.com -
i have been working on of morning no end. trying execute button uses onclick java in salesforce.com , keeps throwing errors. think issue may special characters in data works when use text. time numbers or special characters present error "unexpected token illegal". can me see doing wrong , how can away failing when special characters involved?
{!requirescript("/soap/ajax/28.0/connection.js")} var opptyobj = new sforce.sobject("opportunity"); var caseobj = new sforce.sobject("case"); var today = new date(); var sopptyid = "{!case.opportunity__c}"; if( sopptyid != "") { alert("this case tied opportunity!"); } else { opptyobj.accountid = "{!case.accountid}"; opptyobj.closedate = sforce.internal.datetimetostring(today); opptyobj.description="{!case.description}"; opptyobj.case__c = "{!case.id}"; opptyobj.name = "{!case.subject}"; opptyobj.stagename = "estimate in progress"; opptyobj.created_from_case__c = "y"; opptyobj.type = "new business"; opptyobj.amount = ".01"; var opptyresult = sforce.connection.create([opptyobj]); if (opptyresult[0].success=='false') { alert("opportunity creation failed: " + opptyresult[0].errors.message); } else { caseobj.id = '{!case.id}'; caseobj.opportunity__c = opptyresult[0].id; caseobj.status = "estimate in progress"; var caseresult = sforce.connection.update([caseobj]); if(caseresult[0].success == 'false') { alert("case update failed: " + caseresult[0].errors.message); } else { alert("an opportunity has been created , linked case."); location.reload(true); } } }
assuming kind of template, whatever rendering needs escape values in strings it's inserting.
given this:
opptyobj.description="{!case.description}";
let's enter description consisting of this:
"that awesome," said john.
when rendered in template result this:
opptyobj.description=""that awesome," said john.";
as might able see, result syntax error.
you need escape quote characters in text inserted way. , without knowing technology rendering template can't give specifics, want replace "
\"
, '
\'
. \
escapes characters, forcing them treated literal characters in string instead of other special meaning.
this must done it's being inserted script. in spirit of this:
opptyobj.description="{!case.description.replace(/'/, "\\'").replace(/"/, '\\"')}
exactly how depends on language or template engine being used here. th eresult should this:
opptyobj.description="\"that awesome,\" said john.";
ruby on rails implements escape_javascript
method, sanitizes data injection javascript. following replacements. seems baseline.
'\\'
=>'\\\\'
'</'
=>'<\/'
"\r\n"
=>'\n'
"\n"
=>'\n'
"\r"
=>'\n'
'"'
=>'\\"'
"'"
=>"\\'"
update:
according this: http://www.salesforce.com/us/developer/docs/pages/content/pages_security_tips_scontrols.htm
it looks want jsencode
function. this, perhaps?
opptyobj.description="{!jsencode(case.description)}";
Comments
Post a Comment