[SalesForce] Dynamic SOQL binding

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.

Best Answer

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.

Related Topic