9
Boolean b1 = true;
Boolean b2 = true;
String b3 = 'b1 && b2';
Boolean b4 = Boolean.valueOf(b3);
system.debug('test Bool '+ b4);

Given a string how can you convert into expression without breaking the sting.

3
  • I'm curious how you want to apply this, or what problem you need to solve?
    – pchittum
    Commented Jan 29, 2013 at 12:20
  • In the above example I will get raw data as as show in b3 and need to evaluate true or false.
    – Ugesh Gali
    Commented Jan 29, 2013 at 12:22
  • Would it help if you convert your conditions into a dynamic query and act on results from that?
    – LVS
    Commented May 3, 2013 at 11:13

4 Answers 4

10

It is possible to mimic a Javascript eval() in Apex by making a callout to the executeAnonymous API method.

There are two common ways you can get a response back from executeAnonymous.

  1. Throw a deliberate exception at the end of the execute and include the response. Kevin covers this approach in EVAL() in Apex. Secure Dynamic Code Evaluation on the Salesforce1 Platform.
  2. I used a variation of this approach but returned the response via the debug log rather than an intentional exception. See Adding Eval() support to Apex.

Using my example the Apex would be something like:

string toEval = 
    'Boolean b1 = true;' + 
    'Boolean b2 = true;' + 
    'boolean b3 = b1 && b2;' + 
    'System.debug(LoggingLevel.Error, b3);';
boolean b4 = soapSforceCom200608Apex.evalBoolean( toEval );
system.debug('test Bool '+ b4);
1
  • You rock! Never known this is possible in Apex!!!! You have my vote up!
    – Patlatus
    Commented Apr 16, 2015 at 8:55
6

I think what you're after doing is creating an expression as a string and then evaluating it and obtaining the result in Apex.

I don't think it is actually possible to evaluate expressions in Apex natively (it would be great if it was, I also wondered the same since we have the Formula functions). I think the best you could do is roll your own expression parser and AND(Boolean, Boolean) function that implements the required behaviour.

4
  • Thank you. Will think of building own expression parser.
    – Ugesh Gali
    Commented Jan 29, 2013 at 12:17
  • No. There are some native features that will evaluate your string such as Database.insert and other DML methods. But not an arbitrary expression.
    – pchittum
    Commented Jan 29, 2013 at 12:19
  • I want do it in apex and expression is much more complex.
    – Ugesh Gali
    Commented Jan 29, 2013 at 12:20
  • 2
    You should probably research the Interpreter design pattern, one thing to obviously be wary of from the start when going down this route would be the limits around script statements, especially if you're going to try and implement complex/recursive expressions. I'd be interested to hear if anyone else has any clever tricks in this area. Commented Jan 29, 2013 at 12:23
1

I created an apex class on github to do this. Take a look at this... https://github.com/hsaraujo/BooleanEvaluate-Apex

List<Boolean> values = new List<Boolean>{
true, false, true, true, false, true, true, true
};

BooleanEvaluate boolEval = new BooleanEvaluate('1 && ( ( 2 || 3 ) && 6 ) && ( 4 && 5 || ( 7 && 8 ) )', values);
boolEval.evaluate();

If someone wants to improve it, please fork! Hope it helps

2
  • 1
    please include the essentials of the link in your post, as is, this is a link only answer, although it might help, if the link becomes invalid, so will your answer. Please update accordingly and take some time to review How to Answer and take the tour. thanks!
    – glls
    Commented Oct 27, 2017 at 2:08
  • Hi @glls, This is my first answer here, thank you for your tips Commented Nov 6, 2017 at 2:12
0

Also you can try Script.apex(https://github.com/Click-to-Cloud/Script.apex)

Map context = new Map{ b1 => true, b2 => false };

Boolean result = (Boolean)ScriptEngine.getInstance().eval('b1 && b2', context);

You must log in to answer this question.

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