Tuesday, 31 July 2012

Handle single quotes within query in javascript

We are facing the problem with single quote characters in string query with filters.When single quotes are inluded in strings (either in filter conditions or searchstring) used in query we handle with replace function as shown below.
Eg: 


var projname=document.getElementById('pg:myForm:pb:newrecord:'+Rowindex+':pInput').value;
Here 'projname' is string with single quotes.
now  we want to use this in query as shown below

sforce.connection.sessionId = '{!$Api.Session_ID}';
 var result = sforce.connection.query("Select Id,Name From Milestone1_Project__c where name ="+"'"+projname.replace("\'","\\'")+"'");
 var records = result.getArray("records");

To retrieve values
if(records.length!=0){
            for(var i=0;i<records.length;i++){
                          alert(records[i].name); 
            }
}

No comments:

Post a Comment

Mastering Agentforce: The Power of Agent Script

Hi, In the rapidly evolving world of AI, the challenge for developers has always been balancing the creative reasoning of Large Language Mod...