I'm fairly new to this. How can I extract a text field String and use it as Boolean in apex logic? For instance, I have a field called Logic__c that contains "mc.Quantity__c == null || mc.Quantity__c == 0". I want to use Logic__c in my apex condition but I don't want to write the actual logic each time. There will be many of these rule logic records so I cannot solve it with validation rules either. Any ideas?

   for(Measure_Product_Rule__c mpru : mProductRuleLst){ 

       //if(mc.Quantity__c == null || mc.Quantity__c == 0 ) {       
       if(Boolean.valueOf(mpru.Rule__r.Logic__c) == true){  

        Measure_Rule__c mToRule = new Measure_Rule__c();            

        if(mProductRuleLst.size()> 0) {     
            mToRule.Rule__c = mpru.Rule__c;             
            mToRule.Type__c = 'Automatic';                                                                                           


1 Answer 1


Apex does not have an eval() function like you might find in JavaScript or other languages. You would need to write the parsing and evaluation logic yourself, and it would be a not inconsiderable project.

Since Apex doesn't have support for introspecting the current scope, you wouldn't be able to expose arbitrary variables to your expressions in any case.

There is some prior art you could look at to get a sense of what goes into this kind of a project:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .