You need to sign in to do that
Don't have an account?

How to input a string variable into a SOQL Query
Hi All, I have met a problem and have no idea how to handle it. Here is the situation: I need to write a SOQL sentence like "SELECT field1 FROM object1" but in my case I don't know the value of fields1 in the first place. There is a string to hold the value. How can write the SOQL sentence to use this string value. Thanks
hello ,
u can make query in string where you store the api name in the string and pass that string into that strong query and execute using database.query();
e.g.
String field = 'fieldx__c';
Strign soqlQuery = 'select Id,'+field+',Name from object__c';
object__c obj = database.query(soqlQuery);
try this one .you will get your solution.
All Answers
here is an example
String para='App-0009';
Applicant__c app;
app=[select Name, Name__c, Status__c from Applicant__c where Name LIKE :para limit 1];
I hope this will help you..
Thanks for your quick reply. But this is not what I want. Let me clarify my question more clearly. For example, I have lots of fields in one object, field1, field2.....field100. And I don't know which field I need to select in the first place. But there is a string can have this value. ie. String myString = 'fieldx' But how can I use this string in my soql sentence. Thanks
hello ,
u can make query in string where you store the api name in the string and pass that string into that strong query and execute using database.query();
e.g.
String field = 'fieldx__c';
Strign soqlQuery = 'select Id,'+field+',Name from object__c';
object__c obj = database.query(soqlQuery);
try this one .you will get your solution.
Thanks for your reply. I got one further question. How can I use this field for operation. ie. in the example you give me. Can I do thing like " obj.'+field+' = Value" ?
Hello,
do you want use it in query? you can use after all the value will get replaced so it will work as our SOQL query.
Thank you.
No,I want to use it out query. For example, I want to put a value in it after the soql query. How can I realize it. Can I do just like normal like Object. field = Value ? Thanks
Hello,
No you can not do like that.
because if you pass any variable object take it as its field and it is not allowed there.
Regards,
Minkesh Patel
Then, is there any way to realize it? Coz I want to put some value in this unknown field. Thanks
Hello,
do you want to use it in query after assigning that field to the variable? e.g.
String s = 'filedx__c';
and you are using this field in String query than and than you can use like i suggest you.but if you want to use directly using obj.'field' than you can not use.
Regards,
Minkesh Patel
I am not quite fimilar with SOQL. How to initial a value in a field through SOQL query. e.g
String myQuery = 'SELECT Id,'+fieldx+' FROM Obj';
if i want to initial a value of the fieldx in the query. How can I change the above query to make it works. Thanks a lot
You mean like this?
String myField = 'SomeField__c';
String myValue = 'My Value to query';
String query = 'select' + myField + ' from MyObject__c where SomeOtherField__c = :myValue';
List<sObject> oList = Database.query(query);
Dear dnakoni,
Thanks for you reply. Not quite like this. The result I need is put a value in "myField" . So in your example, what i need is put "My value to query" into myField which is the Dynamic field. Because I need to dynamiclly update a record but one of its field is unknown. How can i realize this. Thanks
kind regards
Kevin
Ah, got it, sorry. Since you cannot update the field in the actual select statement (since, well, it's a query, not an update), here's how to do it:
This code puts the same value ('My Value') into every sObject's SomeField__c that was received from the query, and then updates them in the database.
Let me know if you have any more questions.
Great. Many thanks for your help. :)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm
This is what I have according to the answer by Minkesh.
String test=Criteria.CustBusinessPro__Where_Clause__c;
String applicantQuery=[SELECT Id,Name FROM CustBusinessPro__Applicant__c WHERE :testWhere];
CustBusinessPro__Applicant__c app=database.query(applicantQuery);
But it shows error in syntax.Please help.
Refer to this link for more help :https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm
I have something like this in my string testWhere:
Age>23 AND salary>8000000
I just want to put this string after where.
How can I achieve that?
This might solve your question: Some of the below points might help:
1st: database.query takes string value
2nd: SOQL queries returns one or more results, so use list
3rd: check your query first using system.debug for any errors, and execute the same SOQL query in dev console to find any errors
Hope this helps.
I am alos writing coding similar to this. I even passed the object in the string to select the fields.
I passed the field name and the object from the Auro component to retrieve values and display it to the dropdown. This one method is called twise in my program. And I also create a separate Aura component to invoke this apex to reduce the coding.