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
SimrinSimrin 

Using variables for SOQL where clause

Hello,

I have a snippet like below,
public with sharing class customClass{

	public List<CustomClassOne> x{get;set;}

	public customClass(){
		x = Select column1 From CustomClassOne; 

		for(CustomClassOne counter : x) {
			Integer i;
			i = Select count() From CustomClassTwo where column2=counter; 
		}
	} 
}
How can i set column2=counter in where clause ?
Best Answer chosen by Simrin
Bhanu MaheshBhanu Mahesh
Hi Simin,

To add a variable in WHERE clause you have to use ':' before the variable
Select count() From CustomClassTwo where column2=:counter;

The requirement is not clear to suggest.

Assuming you are trying to find the count from CustomClassTwo  where column2 is equal to the column1 of CustomClassOne.
 
public with sharing class customClass{

	public List<CustomClassOne> x{get;set;}

	public customClass(){
		x = [Select column1 From CustomClassOne]; 

		for(CustomClassOne counter : x) {
			Integer i;
			i = [Select count() From CustomClassTwo where column2= :counter.column1]; 
		}
	} 
}


Regards,
Bhanu Mahesh