You need to sign in to do that
Don't have an account?
Custom Search to ignore null values
public with sharing class contactSearchExtension { public contactSearchExtension(ApexPages.StandardSetController controller) { } public contactSearchExtension(ApexPages.StandardController controller) { } public String getName() { return null; } public String getState() { return null; } public List<Contact> result = new List<Contact>(); private String Name; private String State; public contactSearchExtension(){ } public List<Contact> getResult() { return result; } public void setName(String Name) { this.Name = Name; } public void setState(String State) { this.State = State; } public void search() { String queryName = '%' + Name + '%'; String queryState = '%' + State + '%'; result = [ SELECT Account.Name, Name, MailingState FROM Contact WHERE Account.Name like :queryName AND MailingState like :queryState]; } }
Almost try this.
public void search() {
String queryString = 'SELECT Account.Name, Name, MailingState FROM Contact ';
if (Name != null && State == null){
queryString += WHERE Account.Name like %\'' + Name + '\'%';
}else if(Name != null && State != null){
queryString += WHERE Account.Name like %\'' + Name + '\'and MailingState like '\'% + State + '\'%';
}else if(Name == null && State != null){
queryString += WHERE MailingState like %\'' + State + '\'%';
}else{
queryString += 'Limit 10';//this limits the query so they get something back. You can change this to return an error.
}
try{
result = Database.query(queryString);
}catch(QueryException e){
ApexPages.addMessages(e);
}
}
Please keep in mind this code is not tested and I did not try to compile it.
This should put you on the right track.
All Answers
The Apex feature you need to use is called Dynamic SOQL. Please review this part of the Apex docs.
What you want to do is create the query string variable dynamically based off of what the user fills out or not.
Please post any issues or questions you have when trying to use dynamic soql.
Thanks for the advice.
Am I on the right track with something like this?
It still hasnt given me the result I am after, but am I getting close or completely off target?:
public void search() { String queryName; if (Name == null){ queryName = ''; } else { queryName = '%' + Name + '%'; } String queryState; if (State == null){ queryState = ''; } else { queryState = '%' + State + '%'; } result = [ SELECT Account.Name, Name, MailingState FROM Contact WHERE Account.Name like :queryName AND MailingState like :queryState]; }
Almost try this.
public void search() {
String queryString = 'SELECT Account.Name, Name, MailingState FROM Contact ';
if (Name != null && State == null){
queryString += WHERE Account.Name like %\'' + Name + '\'%';
}else if(Name != null && State != null){
queryString += WHERE Account.Name like %\'' + Name + '\'and MailingState like '\'% + State + '\'%';
}else if(Name == null && State != null){
queryString += WHERE MailingState like %\'' + State + '\'%';
}else{
queryString += 'Limit 10';//this limits the query so they get something back. You can change this to return an error.
}
try{
result = Database.query(queryString);
}catch(QueryException e){
ApexPages.addMessages(e);
}
}
Please keep in mind this code is not tested and I did not try to compile it.
This should put you on the right track.
I'll have a a play around with that and let you know how I go.
Thanks!
Thanks so much. I am fairly new to developing, so this was a great help.
I got it to work perfectly.
In the end I took your concept and did it the long way (it just made more sense to me as a newbie).
Do you see any issues with running it this way?
public void search() { String queryName = '%' + Name + '%'; String queryState = '%' + State + '%'; if (Name == '' && State == ''){ result = [SELECT Account.Name, Name, MailingState FROM Contact]; } else if (Name != '' && State == ''){ result = [SELECT Account.Name, Name, MailingState FROM Contact WHERE Account.Name LIKE :queryName]; } else if (Name != '' && State != ''){ result = [SELECT Account.Name, Name, MailingState FROM Contact WHERE Account.Name LIKE :queryName AND MailingState like :queryState]; } else if (Name == '' && State != ''){ result = [SELECT Account.Name, Name, MailingState FROM Contact WHERE MailingState like :queryState]; } }
Thanks again for your help.
You've saved me a lot of time and probably from going prematurely grey/bald/both!