This question already has an answer here:

I have a string conditions logic that retrieves values for a Map<String,Boolean> and checks if logic is true or false. For example my logic looks like:


Being A => AND and O => OR.

And my map has this kind of structure:

new Map<String,Boolean>{'1'=>true,'2'=>false,'3'=>true};

Is it possible to replace characters in logic conditions with boolean values and get a proper result in Apex? Or even in Javascript?

marked as duplicate by Mr.Frodo, Sebastian Kessel, Lukas Lunow, Raul, Derek F May 15 at 12:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Answer of your question:

No even if you replace complete String with actual expression there is no way you can parse it. For parsing the expression you would need to write the parser.

Write Parser:

You are actually evaluating expression and expressions are evaluated using Stack (generally).

Basically here you would need to build a stack where you will put each character in stack and then start popping those characters until you find a matching closing bracket, by that way you can calculate it with much less complexity.

Go through this algo for more detail.

It is already discussed in this thread, more easier solution could be using the executeAnonymous API although i have never implemented this myself. Refer Daniel Ballinger's this blog post for more details.

  • Can you give a more specific example? Cause I'm pretty lost with this... Thanks in advance! – molinet May 14 at 15:41
  • @molinet I have added some links which provides more ways of doing it. – Mr.Frodo May 14 at 15:55
  • Thanks a lot! Now I understand it better and managed to achieve the requirement. – molinet May 16 at 8:31

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