+ Start a Discussion
JamesSSJamesSS 

Include quote in query

I am using dynamic query in my code.

 

My code:

queryString += '( Type__c <= '+mySearch +')';

My error:


System.QueryException: value of filter criterion for field 'Type__c' must be of type string and should be enclosed in quotes

 

How to include quote in above code?

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi James,

 

Try to use below code.

 

queryString += '( Type__c = \''+mySearch +'\')';

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

please post your full soql query

JamesSSJamesSS

String queryString = 'SELECT Id FROM Account WHERE';

if(mySearch != null)
queryString += '( Type__c = '+mySearch +')';

List<Account> AccountList = new List<Account>();
AccountList = Database.query(queryString);
system.debug(Logginglevel.ERROR,'*******AccountList*********'+AccountList);

My querystring output in log:
SELECT Id FROM Account WHERE Type__c = 2013

My expected output:
SELECT Id FROM Account WHERE Type__c = '2013'

souvik9086souvik9086

Try this

 

queryString += '( Type__c = \''+mySearch +'\')';

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

hitesh90hitesh90

Hi James,

 

Try to use below code.

 

queryString += '( Type__c = \''+mySearch +'\')';

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
sfdcfoxsfdcfox
Don't forget to use String.escapeSingleQuotes on mySearch to avoid SOQL injection.