0

In the List View we can see the Filter logic that enable us to handle many condition such and 1 AND (2 OR 3) . But I m not able to figure out how this logic is being handled in apex.

For example if we let the user to input 1 AND (2 OR 3) , how we apply statement in our query? Do our code need to read 1 , AND,(,2,OR,3,) or we can simply put this in string?

Anyone familiar with this? Can you share how to do this?

  • 1
    Hi, let's take same example 1 AND (2 or 3 ) on Case object 1 as Name field 2 as Origin field and 3 as Priority field so based on this back end it will frame Select Id, Name, Origin Priority from Case where Name = 'Test' AND (Origin = 'Phone' OR Priority = 'Medium') like that query will frame and will retrieve data. – Prem Anandh Aug 7 '18 at 4:55
  • If you are new to Apex and SOQL, then Trailhead is a good place to start. The SOQL documentation also has a very clear explanation. – David Cheng Aug 11 '18 at 7:02
1

let's take same example 1 AND (2 or 3 ) on Case object 1 as Name field 2 as Origin field and 3 as Priority field so based on this back end it will frame below query.

Select Id, Name, Origin Priority from Case where Name = 'Test' AND (Origin = 'Phone' OR Priority = 'Medium')

Selected field is based on your selection in list view (here I just used Id, Name, Origin, Priority)

1

To add to Prem's answer, Apex supports dynamic SOQL where the query is a string and so the string can be built to match the necessary pattern.

So somewhere in the back-end logic, the place keepers "1" and "2" and so on are replaced by the conditions taken from the user interface and appending to the query string with the brackets and tokens like "AND" and "OR" kept.

I would guess that someone has built this logic before, and if your google-fu is strong, you may be able to find some code to start from.

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.