You need to sign in to do that
Don't have an account?
URVASHI
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.
It will be like this
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
It will be like this
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Hey thanks a lot...i am getting the desired output.
But can u please explain the String n use of '\' for achieveing it.
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
@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:
What an absurd syntax.Thanks vbs for the simple method!
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+'\''
String query='select column_name from information_schema.COLUMNS where table_name = :selectedValue1';
String query4 = 'SELECT count() FROM FieldDefinition WHERE EntityDefinition.QualifiedApiName =: obj';
trigger.new[0].Number_Of_Total_Fields__c = Database.countQuery(Query4);