2

I have some filter conditions on my page and when user selects those filter conditions I need to display all those records that satisfy those filter conditions

like suppose these are the filter conditions that user selects

(Account name = 'Account1') AND (Contact name = 'Contact1')

now I need to put that in where clause of SOQL query. I was wondering how to do that

[Select id from Object where] I need those filters to be there after where. Any help or any other workaround is much appreciated. Thanks.

1
  • What have you tried so far? Could you post your non-working code as a starting point? It is not clear how the user is selecting the filter conditions.
    – JimRae
    Feb 27, 2014 at 16:50

4 Answers 4

1

Hold on, I have just read your comment on the other answer.

You already have your filter criteria built up into a string?

In that case, you need to build the rest of your query into the string, and use Database.query like so:

// This deduced however you like
String filterValue = '(accountname = \'account1\') and (contactname =\'contact1\')';

String queryHeader = 'SELECT Id FROM Object WHERE ';
List<sObject> results = Database.query(queryHeader + filterValue);

Or any other manipulation of strings that you like.

You can find more out about dynamic SOQL here.

2
  • I did what you said but I am getting the following error. System.QueryException: unexpected token: ==
    – cartman
    Feb 28, 2014 at 4:53
  • Could you post your actual query maybe? I'm not sure from that if you have used a double equals '==' which is not valid, or you are having a problem with your fields/relationships (which is normally the cause of a '_' error) Feb 28, 2014 at 9:24
1

The SOQL is a very SQL like query language, so you can just use:

Select id from Object 
Where AccountName__c = 'Account1'
And ContactName__c = 'Contact'

If these fields are lookup fields to Account and Contact object, use relationship names:

Select id from Object 
Where Account__r.Name = 'Account1'
And Contact__r.Name = 'Contact'

You can read more about SOQL here: Condition Expression Syntax (WHERE clause)

1
  • @mastor: you got my question wrong, I am storing that account and contact condition in a variable like filtervalue = (accountname = 'account1') and (contactname ='contact1'). Now I need to put that filter value in the where clause . Everything should happen dynamically.Thanks.
    – cartman
    Feb 27, 2014 at 16:02
1

If you want to include instance variables in SOQL queries, you can do so using a colon.. like so:

String filterText = 'srlawr';
List<Contact> contacts = [SELECT Id FROM contact WHERE name = :filterText];

It's that simple!

In your example, something like:

[Select id from Object where Account__r.name = :Account1 AND Contact__r.name = :Contact1]

(assuming of course you have the appropriate relationships in your object, and the variables Account1 and Contact1)

Note: no apostrophes.

0

Use '+' many times as below -

[select ID,Name, account.name from Contact where account.Name like :+'%'+searchboxstring+'%']

Pasting my sample code below -

public class democlass {
 public string searchboxstring{get; set;}
 public list<contact> result {get; set;}

 public democlass()
 {
  result = new list<Contact>();
 }
 public void searchbox(){
  result=new List<Contact>();
     result=[select ID,Name, account.name from Contact where account.Name like :+'%'+searchboxstring+'%'];
 }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .