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
kevinjia1984kevinjia1984 

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

Best Answer chosen by Admin (Salesforce Developers) 
minkeshminkesh

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

Chamil MadusankaChamil Madusanka

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..

 

kevinjia1984kevinjia1984

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

minkeshminkesh

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.

 

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney
Like Minkesh has suggested above, dynamic soql is the way to do it.
kevinjia1984kevinjia1984

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" ? 

minkeshminkesh

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.

kevinjia1984kevinjia1984

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

minkeshminkesh

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

kevinjia1984kevinjia1984

Then, is there any way to realize it? Coz I want to put some value in this unknown field. Thanks

minkeshminkesh

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

kevinjia1984kevinjia1984

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

dnakonidnakoni

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);

kevinjia1984kevinjia1984

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

dnakonidnakoni

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:

 

 

String myField = 'SomeField__c';
String myValue = 'My Value';
 
String query = 'select' + myField + ' from MyObject__c where....';
 
List<sObject> oList = Database.query(query);


for (sObject so : oList) {
   so.put(myField, myValue);
}

update oList;

 

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.

 

 

kevinjia1984kevinjia1984

Great. Many thanks for your help. :)

Prasad LakhangaonkarPrasad Lakhangaonkar
Use colon to bind the local varible in SOQL query
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm
Revati BhavsarRevati Bhavsar
I want to put a string value into the where part of the query?How to do that?
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.
Prasad LakhangaonkarPrasad Lakhangaonkar
You are using different variables for initalization and in where clause, also you are not putting any condition in where clause 

Refer to this link for more help :https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm
 
Revati BhavsarRevati Bhavsar
hello Prasad_Lakhan
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?
Prasad LakhangaonkarPrasad Lakhangaonkar
Hello Revati,
This might solve your question:
//Original code
//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);


/modified
String test=Criteria.CustBusinessPro__Where_Clause__c;
String applicantQuery='SELECT Id,Name FROM CustBusinessPro__Applicant__c WHERE'+' '+test ;
System.debug('query:'+applicantQuery);
 List<CustBusinessPro__Applicant__c> app=database.query(applicantQuery);
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.
 
Pargavi Selvaraj 7Pargavi Selvaraj 7
Hi
I am alos writing coding similar to this. I even passed the object in the string to select the fields. 
@AuraEnabled        
    public static List<sObject> getValuesLoanPrd(String objectType, String selectedField){
        String quer1='SELECT '+ selectedField +' FROM '+objectType;
        List<sObject> obj1=database.query(quer1);
        return obj1;
    }

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.