function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jawtechjawtech 

javascript eval in Apex?

Is there any way using any tools to evaluate javascript code (a retrieved string) inside Apex? Ideally I'm looking for something similar to the eval function of javascript but run within Apex.

 

thanks!

 

James

 

Cloudbench Applications

providers of BasicGov.com

 

gm_sfdc_powerdegm_sfdc_powerde
Apex is a strongly typed language and does not support evaluating dynamic content.  Having said that, it is possible to dynamically construct and execute Apex code from outside the platform using web service API.  Not sure if this is possible in your case, can you elaborate more on your use case?
Cool_DevloperCool_Devloper

No tools as such, but you have to write the equivalent logic using Apex.

I am afraid, there is no direct function similar to EVAL:(

Experts, correct me if I am wrong.

Cool_D

sfdcfoxsfdcfox
All the prior answers here are indeed correct. There is no "EVAL" in Apex, due to it's strong typing mechanisms. you can use the Sobject object to manipulate any object, and Database methods provide dynamic query functionality. It would be possible (albeit not necessarily feasible) to write a helper class that could implement simple scripting, but this is definitely not built in to the language at the moment.
JWikkJWikk

Thanks for the replies everyone. It saved me the headache of prototyping something. We were using scontrols to pull scripted functions stored in the database and then running them inline. That way each of our clients can customize their own behaviour without us writing custom code for each organization. It was very cool because it was used to build a much more complicated workflow engine which we now implementing in Apex.

 

I guess I'll set up a webservice that receives the javascript, and parameter values, execute it and returns the result. A lot more work compared to scontrols. I know scontrols can call Apex, but not the other way around :(