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
URVASHIURVASHI 

Apex query passing string variable within quotes

 i need to pass selected value with ' ' in the following string but i am unable to do so

 String query='select column_name from information_schema.COLUMNS where table_name ='+ selectedValue1;

 my debug log shows the below mentioned statement

select column_name from information_schema.COLUMNS where table_name =RegistrationTable

 but the actual required statement should be 

select column_name from information_schema.COLUMNS where table_name ='RegistrationTable'

 how do i prepare my string variable. As Sfdc is not allowing me to use "" to prepare the string.

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

It will be like this

 

String query='select column_name from information_schema.COLUMNS where table_name =\''+                                 String.escapeSingleQuotes(selectedValue1)+'\'';

 

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

Thanks

All Answers

souvik9086souvik9086

It will be like this

 

String query='select column_name from information_schema.COLUMNS where table_name =\''+                                 String.escapeSingleQuotes(selectedValue1)+'\'';

 

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

Thanks

This was selected as the best answer
URVASHIURVASHI

Hey thanks a lot...i am getting the desired output.

 

But can u please explain the String n use of '\' for achieveing it.

 

 

souvik9086souvik9086

The use of \' is to append the quote around your selected value. For e.g you want

 

Name = 'Test'

 

then in the query 

val = 'Test';

String qry = 'select name from contact where Name =' + val;

This will return the query as

 

[SELECT name from contact where Name = Test]

which is wrong.

What is needed is

 

val = 'Test';

String qry = 'select name from contact where Name =\'' + String.escapeSingleQuotes(val)+'\';

As you append \' around your value then the output will be as

[SELECT name from contact where Name = 'Test']

which is correct

 

If the post helps you please throw KUDOS

Thanks

 

vbsvbs

@Urvashi - I know this has been marked as resolved but just for anyone else reading this post - There is an easier way for doing this without escaping quotes. If this variable is a simple variable i.e. does not make use of dot(.) notation then they can be embedded in the string and will be read as a bind variable by the query. Try thi:

 String query='select column_name from information_schema.COLUMNS where table_name = :selectedValue1';
david roberts UKdavid roberts UK
I spent hours on this!
What an absurd syntax.Thanks vbs for the simple method!
david roberts UKdavid roberts UK
you also don't need the function String.escapeSingleQuotes.
The sequence \' represents the single quote that needs to be either side of the variable val.
In effect, it says "escape quote"
so you need 'query string WHERE name = ESCAPE QUOTE' + val + 'ESCAPE QUOTE'
i.e....WHERE name = \''+val+'\''
Suman sfdcSuman sfdc
Great vbs Sir, It works.....
String query='select column_name from information_schema.COLUMNS where table_name = :selectedValue1';
Suman sfdcSuman sfdc
I have used like this in Trigger.. 
String query4 = 'SELECT count() FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName =: obj';
trigger.new[0].Number_Of_Total_Fields__c = Database.countQuery(Query4);