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
krixkrix 

How to use a local code variable (preceded by a colon) in SOQL query string (Database.query(str))?

Hi!

 

I made a procedure that creates an SOQL query string. It occurs dynamically beacause it has user input dependency where clause. The where clause contains an IN operator with an apex local code variable (preceded by a colon). How can we use this variable in the query string?

 

The problem reduced: How to create this with query string?

List<T__c> L = [SELECT id, status__c FROM T__c WHERE status__c IN :tmpList];

This is not okay:

List<T__c> L = Database.query("SELECT id, status__c FROM T__c WHERE status__c IN " + tmpList );

 

Thanx, Krix

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Check out this blog post (not mine) for examples:

 

http://www.nimbleams.com/blog/2013/1/29/variable-binding-in-dynamic-soql/

All Answers

bob_buzzardbob_buzzard

Check out this blog post (not mine) for examples:

 

http://www.nimbleams.com/blog/2013/1/29/variable-binding-in-dynamic-soql/

This was selected as the best answer
krixkrix

Thanx for the quick amswer!