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
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.