-1

I need to dynamically calculate a formula in APEX, the formula will be constructed as a string. Consider something similar to JavaScript eval() function.

For example I need to calculate value for ((2*3)+5), this value will be different in different use cases. Is there a way APEX allows one to do this natively?

Please note that I cannot do this calculation on a VF page else I would have used JS already. This has to happen in an APEX class/trigger.

  • It would help if you were to go into more detail about what you're trying to accomplish. Where are the values you're trying to calculate coming from? Given that you say this has to be in a trigger, I imagine that the values are stored in some field somewhere. If all of your fields are present on a single record, then a formula field can help. If you want to aggregate values for records related to a common parent, then some flavor of rollup (traditional, or with the DLRS tool) can help. – Derek F Dec 4 '17 at 13:44
  • @DerekF, I cannot use formula field in my scenario as its going to be dynamic formula(to be entered by user) and that formula can use field value of multiple records. So I need to identify some way of calculation these dynamic values using apex itself. – user34950 Dec 7 '17 at 5:24
1

APEX doesn't support anything like this natively but you can look at Apex EvalEx and evaluate if it suits your requirements

Thanks!

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.