By now we’re well into the Spring ’13 pre-release cycle: the release preview webinar is on Wednesday, selected sandboxes and NA1 already have the new release, and the remaining instances will be updated over the next couple of weekends. Techie that I am, my favorite new feature is the Tooling API, generally available (GA) in Spring ’13.
- Create/read/update/delete code artifacts such as Apex Classes and Triggers, and Visualforce Pages and Components,
- Manage debugging features such as trace flags, heap dump markers and Apex/SOQL overlays
- List debugging artifacts such as debug logs and heap dumps
As a demo for the upcoming webinar, I wrote a simple Apex Class browser/editor as a Java web application, showing just how to work with the REST Tooling API. Since I’m a somewhat polyglot programmer, I tend to use REST, even from Java, since I can quickly hack things out from the command line with curl, then code it up using Simple JSON, but many Java and C# developers prefer the SOAP API. If you’re in that camp, then from your org, go to Your Name | Setup | Develop | API, download the Tooling API WSDL, generate typed proxies, and use the appropriate
retrieve() etc method calls.
In any case, my code is all on GitHub, and if you have a pre-release login, you can go try it out on Heroku. Here are a few snippets; they all use the
ToolingApi class, a very thin layer over Apache HttpClient that creates an HTTP request with the correct endpoint, access token etc, sends it, and parses the JSON response. In particular,
ToolingApi prepends the instance URL and Tooling API prefix, for example
https://na1.salesforce.com/services/data/v27.0/tooling/, to the URL path. Note also, that, to keep things simple, I haven’t shown error handling here, but you can find it in the ApexClassesController class.
Use SOQL SELECT to retrieve a list of
To create an Apex Class, post JSON containing the class name and body to
Since a change might involve several code artifacts, update is a little more involved. You will need to create a
MetadataContainer to hold
ApexComponentMember objects, then create an
ContainerAsyncRequest to compile and deploy the new code. Here’s a simple example with a single
The next few releases will bring additional Tooling API functionality – access to more resources, such as debug logs and code coverage results, and more operations, such as execute anonymous blocks of code, and run tests. We will also rewrite the Force.com IDE (Eclipse plugin) to use the Tooling API, and open source it for you to use as a template for adding Force.com capability to your favorite development environment.