+ Start a Discussion
AWL-CRMAWL-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.

 

 

sdetweilsdetweil

there is currently no apex eval() function.. ( I need one too!)

 

 

AWL-CRMAWL-CRM

Thank you for your reply.

 

In this case, is there any other solution to solve my problem?

Rahul SharmaRahul Sharma

Can you explain your problem in brief?

AWL-CRMAWL-CRM

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.

 

List<String> expressions;

// Some Processing on data to fill expressions
// e.g. expressions[0] = 'true AND false'
// e.g. expressions[1] = '(true AND true) OR false'

List<Boolean> ret = new List<Boolean>();

for(String s : expressions)
{
   ret.add(eval(s)); // ?????
}

for(Boolean b : ret)
{
   if (b)
{
// Some processing
}
}

 

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.

 

Controller:

@RemoteAction
public static List<String> first()
{
List<String> expressions = new List<String>();
  //Build list of expression via processing on data return expressions; }

public PageReference handleReturn()
{
String returnedList = Apexpages.currentPage().getParameters().get('returnedList');
// Processing
}

Visualforce page :

<apex:page standardController="XXX" extensions="YYY">
    <script>
    window.onload = function()
        {
            if (!{!evalDone})
            {
                var lstRet  = '';
                var idValue = '{!param}';
               
                YYY.first(idValue, function(result, event)
                {
                    var listStr = result;

                    for (var x = 0; x < listStr.length; x++)
                    {
                        var res = eval(listStr[x]);
                        lstRet = lstRet + res;
                        lstRet = lstRet + ';';
                    }
                   
                    handleReturn(lstRet);
                }, {escape:true});
            }
        }
</script>
    <apex:form >
        <apex:actionFunction action="{!handleReturn}" name="handleReturn" rerender="view">
            <apex:param id="aname" name="returnedList" value="" />
        </apex:actionFunction>
...
</apex:form>
</apex:page>

 

Daniel BallingerDaniel Ballinger
Yes, sort of. 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 (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:
boolean evalResult = soapSforceCom200608Apex.evalBoolean(
                    'boolean result=(true AND false) OR true; System.debug(LoggingLevel.Error, result);');
Heitor Araujo 5Heitor Araujo 5
Take a look at this ApexClass that I developed.
https://github.com/hsaraujo/BooleanEvaluate-Apex