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
    Commented Jun 18, 2015 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...
    – Rajasekhar
    Commented Jun 18, 2015 at 20:06

2 Answers 2

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
    – Rajasekhar
    Commented Jun 18, 2015 at 20:12
  • this is working perfectly...
    – Rajasekhar
    Commented Jun 19, 2015 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
    Commented Jun 18, 2015 at 20:21
  • sure... it would be really great...
    – Rajasekhar
    Commented Jun 19, 2015 at 1:51

You must log in to answer this question.

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