2

enter image description here

I am trying to implement multiple filters on visual force page to generate custom reports by using external system data from rest call.I am not going to store anything in salesforce objects.So i have to do search by looping throught data

like

if(searchField1=searchVal1 && searchField2=searchVal2 || searchField3=searchVal3)){
    system.debug('***'+criteria matched);
}

but the problem is i have to change the && and || operators according to user selection from drop-down like if user selects OR and And in dropdown it has to chnage as

if(searchField1=searchVal1 || searchField2=searchVal2 && searchField3=searchVal3)){
    system.debug('***'+criteria matched);
}

so i was wondering is there any way to achieve this dynamically...

like

if(searchField1=searchVal1 'operator1' searchField2=searchVal2 'operator2' searchField3=searchVal3)){
        system.debug('***'+criteria matched);
    }

happy to hear any changes in the code...

2
  • 2
    If you check some similar Salesforce UI its a bit more involved than you show. Its unclear whether your screen represents (a && b) || c or a && (b || c).
    – Keith C
    Jun 18 '15 at 20:01
  • @KeithC (a & & b) || c in this case... I too feel there are too many complications here... For time being I was struggling to pass operators dynamically...
    – abey
    Jun 18 '15 at 20:06
2

Ignoring the issue mention in the comment, you can create methods that allow this sort of code:

if (op(searchField1 == searchVal1, operator1, searchField2 == searchVal2) ...

where the method would be:

private Boolean op(Boolean b1, String operator, Boolean b2) {
    return operator == 'and' ? (b1 && b2) : (b1 || b2);
}
2
  • Seems to be this was the best thing I have come across... Thanks @keith C
    – abey
    Jun 18 '15 at 20:12
  • this is working perfectly...
    – abey
    Jun 19 '15 at 6:36
1

Without copy-pasting the answer, you'll want to make a Boolean tree (B-Tree), and parse it all the way through. I started writing an answer, and then I realized that there's already a perfectly good answer over here. The only difference is there's some translation you'll have to do going from C# to Apex Code, which should be trivial.

2
  • 1
    And now I feel like writing one... I might be back.
    – sfdcfox
    Jun 18 '15 at 20:21
  • sure... it would be really great...
    – abey
    Jun 19 '15 at 1:51

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.