Tuesday 27 January 2015

EXECUTING ANONYMOUS APEX INSIDE ECLIPSE

One of the most underutilized programming techniques in Force.com is the ability to execute a snippet of apex code "anonymously". I know, it has a funny ring to it. Anonymous code is basically just a block of code that doesn't get stored inside Salesforce.com. This is nice, because often times you just want to try part of a method out or execute a SOQL statement without going through the hassle of adding it to a trigger or a class.

This is a great technique to quickly evaluate or debug a statement of code on-the-fly. There are a couple of ways you can do this.


Open up the Developer Console inside Salesforce
The executeAnonymousWeb services API call
Inside Eclipse/Force.com IDE

I typically do this inside Eclipse so I thought I would quickly highlight how to do that and include a screenshot as a guide. When you have the Force.com IDE plugin installed in Eclipse, all you have to do is navigate to the Execute Anonymous tab. If you don't see the tab, make sure you've got the Force.com perspective open (opens by default when you create a new Force.com project).

Inside the tab, you'll see a dropdown for the active project, a log category dropdown (apex code, apex profiling, callout, database, validation, workflow) and a log level slider.

Below those settings, you'll see a "Source to execute" textbox. Here is where you will actually type your code snippet.

Now all you have to do is run it by clicking "Execute Anonymous". If there are any syntax errors, you'll see those below in the results window. If the code compiles, it will run and you'll see the output below. Don't get discouraged if the output looks a little verbose and hard to read. You can always adjust the slider over to the left and show less information.


No comments:

Post a Comment