1

How to build the above query condition dynamically

(i.e.) if same field label like "email" should be in OR conditions remaining labels should in AND condition

eg... where [((email=:'test@gmail.com')OR(email=:'test_1@gmail.com')) AND ((name=:'testing') OR (name=:'testing_1'))];

9

What I normally do is construct some Set and store there all the possible values you want to compare against.

Set<String> setemailsname = new Set<String> {'test@gamil.com','test1@gmail.com'};
Set<String> setNamesName= new Set<String> {'testing','testing_1'};

[... WHERE email IN: setemailsname AND name IN: setNamesname] ;

You can set up the values to compair against on Custom labels or custom settings and then add those values into the sets instead of using static values like I put in the sample code, but it was just for exemplification purposes.

2

It's worth noting that you can use value collections in pure SOQL with no need for assistance from Apex. The IN operator is the way to go, but it can be used from entirely within the query by wrapping the value collection in parentheses (()):

WHERE Email IN ('value1', 'value2') AND Name IN ('value3, value4')

It takes a bit more care in escaping single quotes, but can yield more portable query strings, which you can then copy to other tools as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.