0

I have a string variable called ifcondition which contains something like '24<25 && 'India'=='India''. I need to evaluate this into an if...else loop.How to do that.

 String ifcondition='if('+test+')';  //Here test contains 24<25 && 'India'=='India'
Boolean result=Boolean.valueOf(ifcondition);
 if(!result)
 {
   System.debug('-----------booleanResult---False');
   }
   else
   {

   System.debug('-----------booleanResult---True');
   }

But it gives false result every single time. Please suggest how to do this and where I am going wrong.Following is a snapshot of debug logs generated.

09:01:33.416 (527789486)|USER_DEBUG|[41]|DEBUG|-------------ifconditionif(21 > 18 && 'Female'=='Female' && 'India' =='India')
09:01:33.416 (527794283)|SYSTEM_METHOD_EXIT|[41]|System.debug(ANY)
09:01:33.416 (527797125)|STATEMENT_EXECUTE|[42]
09:01:33.416 (527814327)|SYSTEM_METHOD_ENTRY|[42]|Boolean.valueOf(Object)
09:01:33.416 (527820266)|ENTERING_MANAGED_PKG|
09:01:33.416 (527830497)|SYSTEM_METHOD_EXIT|[42]|Boolean.valueOf(Object)
09:01:33.416 (527834887)|VARIABLE_SCOPE_BEGIN|[42]|result|Boolean|false|false
09:01:33.416 (527839443)|HEAP_ALLOCATE|[42]|Bytes:5
09:01:33.416 (527847316)|VARIABLE_ASSIGNMENT|[42]|result|false
09:01:33.416 (527852278)|STATEMENT_EXECUTE|[44]
09:01:33.416 (527853743)|STATEMENT_EXECUTE|[45]
09:01:33.416 (527861997)|SYSTEM_METHOD_ENTRY|[45]|System.debug(ANY)
09:01:33.416 (527869108)|ENTERING_MANAGED_PKG|
09:01:33.416 (527871795)|USER_DEBUG|[45]|DEBUG|-----------booleanResult
6
  • Problem is that it would not evaluate that. Check next answer - salesforce.stackexchange.com/questions/113300/… – kurunve Apr 3 '16 at 16:44
  • Looks like you're trying to evaluate your If condition in the same way a query string gets evaluated, but I don't know that you can "cast" a string to a "boolean" test in way that you've done it. It would seem that you'd need to pass those values to classes as strings and integers that return boolean results. Then have a multiple boolean results class of some kind. – crmprogdev Apr 3 '16 at 16:44
  • I dont understand what you are trying to say. Can you elaborate? – Jyoti Zanwar Apr 3 '16 at 16:55
  • Thin the OP is looking for a way to execute an expression defined as a string that produces a boolean result. (See How to execute a code inside an executeAnonymous block of toolingAPI in salesforce?.) – Keith C Apr 3 '16 at 19:53
  • Jyoti, can you tie down more what expression syntax you want to support? Your example just contains constants, but presumably you want to reference data fields? Where does the expression come from - can an end user input it? There are various parsers in e.g. Java that could be ported to Apex or there is the execute anonymous approach but which is appropriate depends on the syntax you need to support (and the risk of dangerous expression being inserted if its a user entered field). – Keith C Apr 3 '16 at 19:57
0

I could do it successfully using following code:

 Boolean b=EvaluateString.evaluateStringToBoolean(test);

It just gave me what I needed avoiding all the complications of ToolingAPI.Hope it helps!

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.