+ Start a Discussion
MRutterMRutter 

Putting a varible into a where clause

I would like to use a variable to define the where clause in a query.

 

I have defined a variable SLName with:

 

SLName=[select id, LastName, spouse_last_name__c from contact

where id = :ApexPages.currentPage().getParameters().get('id')].spouse_last_name__c;

 

How do I make something like the following work?

 

public Contact getSpouseCon(){

if(iscon == null)iscon = [

select FirstName, LastName, spouse_converted__c from contact where Lastname = SLName];

iscon.spouse_converted__c = SLName; //SLName value set properly

return iscon;

 

I am trying to see if a contact exists with the LastName = SLName.

 

Thanks, Mark

Best Answer chosen by Admin (Salesforce Developers) 
micwamicwa

Not sure if I understand what your problem is, but does this help:

 

iscon = [select FirstName, LastName, spouse_converted__c from contact where Lastname = :SLName];

 

 

All Answers

micwamicwa

Not sure if I understand what your problem is, but does this help:

 

iscon = [select FirstName, LastName, spouse_converted__c from contact where Lastname = :SLName];

 

 

This was selected as the best answer
MRutterMRutter

Fantastic, that does exactly what I wanted to do.  Why couldn't I find an example of this is the APEX documention?  It seems like something a lot of people would want to do.

 

Could you tell me more about how the colon is functioning?

 

Also, how do I count the number of records returned from my query?

 

Thanks,

Mark