OK because I'm inherently lazy and I'm not good at formatting google search phrases, I'm just going to ask.

If I were to create a class that has one or more TextArea fields, could I put Apex code in a text area and have some way of dynamically running it in a VisualForce controller?

  • What I was more hoping for is to be able to store a snippet of code in a field. Then run that code from an instance in some way. Might have been a dumb idea. Jun 24, 2013 at 12:48
  • OK so here's the idea. I want to create a class that manages step logic in a flow chart. Kind of like the logic I used to use at a dialer company I worked for. They used a special script and parser to run script at run time to determine what to do based on conditional responses, events, and user input. I'd like to employ something similar but don't want to come up with a scripting language or bloat the web page with lots of JavaScript. Also it would be nice to be able to add snippets of code per step rather than having to code every new flow chart. Jun 24, 2013 at 13:05
  • I have this working with Ajax and JavaScript. Learning things along the way which is good. Now just to add more bells and whistles. Jun 27, 2013 at 21:09

3 Answers 3


No, but you can run code using Execute Anonymous in Developer Console.

  • I stumbled upon some cool features of Anonymous Blocks - declaring methods, classes and exceptions within the block - wrote it up here bit.ly/12U17Vq Jun 22, 2013 at 10:38
  • I'm not trying to manually run the code. I was hoping I could store the code in a field then load it dynamically and execute at run time. This may be easier to do with JavaScript but I'd hate to bloat the page with script and have something that would run server side. Jun 24, 2013 at 20:11
  • 2
    Apex cannot be interpreted at run time, so it isn't possible to store code in a database field and run it. As this post shows, Apex code is first compiled to Java bytecode. Jun 24, 2013 at 20:40

I am a big fan of the free Real Force Explorer. Among other things, it lets you have multiple anonymous apex windows open at once, and to easily run the scripts against different orgs (e.g. Sandbox environments)

You can download from https://bitbucket.org/brsanthu/real-force-explorer/wiki/Home

  • Sorry, I wasn't looking for something I could run test code or test SOQL queries. I was hoping for a way to load some Apex code that could be loaded from a field and then run at run time. It looks like JavaScript might be the best way to do this. Jun 24, 2013 at 20:15
  • So you're effectively going to have to call in through the API ExecuteAnonymous from the page in response to a user event with a payload of apex code which has come from a field? That should work... although I am intrigued what problem you are trying to solve that requires this! Good luck :)
    – Doug B
    Jun 24, 2013 at 20:40
  • I came from writing a dialer and we had a generic state machine that would read in steps that would have simple next, then, and else steps. These could be overwritten at run time with logic from scripting in the step that could handle custom input from a user, inter process communication, and other values you could test. I'm thinking of something similar to write flow charts with Apex and add actions to the tree and elements based on input as well as being able to store the current state and return to where you left off. Jun 24, 2013 at 21:13

Lightside Apps has just released an App on the Appexchange that allows this. 'Power Tools' is a collection of useful tools for Salesforce Admins and Developers and includes a 'Script Library' tool.

The Script Library Tool will allow you to save your apex scripts within a Script record and run them whenever you like, at a nominated time or at a schedule of your choosing. You can also view and optionally store the debug results of the scripts and best of all, no Test Classes are required.

For more information on the Script Library (and the other components), please have a look at the documentation on our website here: http://www.lightsideapps.com/documentation/scripts/index.html

You can download the App with a free 30 day trial on the App Exchange here: https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFoy9UAD

You must log in to answer this question.

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