You need to sign in to do that
Don't have an account?
AWL-CRM
String expression evaluation
Dear all,
here is my problem/question.
In the controller (extension) of one of my pages, I get an expression in a String variable.
I would like to know if the re is any solution for me to evaluate this expression from the String.
Let's say I get something like this:
String s = '(true AND false) OR true';
Is there any function (like the eval javascript function) that would allow me to evaluate my expression?
Boolean ret = eval(s); // true
Thank you everyone in advance.
there is currently no apex eval() function.. ( I need one too!)
Thank you for your reply.
In this case, is there any other solution to solve my problem?
Can you explain your problem in brief?
In fact, I found a solution!
My problem was:
After some processing on data (user and not) I get Strings filled with expression like 'true AND false' or '(true AND true) OR false' in the controller of one of my visualforce pages. From this, the problem was to find a solution to evaluate these Strings and return the boolean result for each of them.
My solution (maybe not the best one but it works):
All the first processing on data is performed in a @RemoteAction static method of my controller.
This method is called by a Javascript (javascript executed on page load) to retrieve the list of expressions to evaluate.
For each String, the script uses its 'eval' function and add the result in a return list.
Finally the script calls another method from the controller (not static this time) via an apex:actionFunction to send back the results which can now be processed in my controller.
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 (https://codefriar.wordpress.com/2014/10/30/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 (http://www.fishofprey.com/2014/11/adding-eval-support-to-apex.html).
Using my example the Apex would be something like:
https://github.com/hsaraujo/BooleanEvaluate-Apex