Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
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.

share|improve this question
    
I'm curious how you want to apply this, or what problem you need to solve? –  Peter Jan 29 '13 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 Jan 29 '13 at 12:22
    
Would it help if you convert your conditions into a dynamic query and act on results from that? –  LVS May 3 '13 at 11:13
add comment

2 Answers 2

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.

share|improve this answer
    
Thank you. Will think of building own expression parser. –  Ugesh Gali Jan 29 '13 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. –  Peter Jan 29 '13 at 12:19
    
I want do it in apex and expression is much more complex. –  Ugesh Gali Jan 29 '13 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. –  Phil Hawthorn Jan 29 '13 at 12:23
add comment

Apex takes its string evaluation mechanisms from Java.

To find out if two strings match, I would suggest using the equals or equalsIgnoreCase methods.

String s1 = ???

if (s1.equals('true')) System.debug('s1 is the exact string true');
else if (s1.equalsIgnoreCase('true')) System.debug('matches TRUE, true, True, tRUE, etc');
share|improve this answer
    
I want to eval the string as JavaScript does. –  Ugesh Gali Jan 29 '13 at 12:13
    
Ah...that's more clear. No support for eval() in Apex. –  Peter Jan 29 '13 at 12:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.