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
SubhamSubham 

Escape Apostrophe in SOQL query

Hi All,

 

I have a custom object where there is a field called lastname which stores name of customer.
Some customer name have apostrophe(') which throws exception while querying
In my controller i am querying to get the value from this field as :
CustomObject__c C = [SELECT lastname__c FROM CustomObject__c ];
and storing it in my controller varible as
string myVariable  = C.lastname;

I get query exceptionas such

 

An Exception has occuredSystem.QueryException: line 1:207 mismatched character '<EOF>' expecting '''Select  LastName , from CustomObject__c Where LastName='O'Testuser' )


This happens when i try to do some operation on customer having apostrophe.

same query works fine for other customer


Please tell me how can i escape unsafe character from a generic query like the above
It's urgent

Regards,

Subham

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

You can use the String.escapeSingleQuotes method before passing the value to soql query.

 

For example:

 

c.lastname=String.escapeSingleQuotes(c.lastname);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

You can use the String.escapeSingleQuotes method before passing the value to soql query.

 

For example:

 

c.lastname=String.escapeSingleQuotes(c.lastname);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
SubhamSubham

Thanks mr jain,,,

It worked

 

Thanks a lot