Apex Variables in SOQL Queries
Bind expressions are used in the WHERE
clause to compare the field’s value with an Apex variable. It uses the following syntax:
WHERE field_name comparisionOperator :bind_variable
Consider this Apex snippet:
String clinicName = 'Orlando';
List<Appointment__c> appointmentList = [SELECT Id, Patient__r.Name
FROM Appointment__c
WHERE Clinic__r.name = :clinicName];
In this example, the name of the clinic is compared with a variable clinicName. You will find it extremely helpful if the code was in an apex method where values were passed as variables.
Example: Design an apex method that returns the maximum number of appointments it can handle in a day given the clinic’s name.
public Integer geMaxNoOfAppointments(String clinicName){
List<Clinic__c> clinicList = [SELECT Id, Name, max_no_of_appointments__c
FROM Clinic__c
WHERE name= :clinicName];
return clinicList [0].max_no_of_appointments__c ;
}
System.debug(geMaxNoOfAppointments('Orlando'));