Apple unveiled a new killer developer tool at this year’s WWDC in the shape of Playground. Playground is a tool designed to accompany their new sparkly program language SWIFT to help developers get to grips with new concepts quickly without the overhead of C. Stop, that’s enough about Apple, I thought this was a Salesforce blog!
Enter the “Salesforce Playground” otherwise known as “Execute Anonymous Apex”. Now imagine if Salesforce had called execute anonymous apex the Salesforce playground – sounds better than Anonymous Apex (Salesforce hint). I suspect most developers on the platform should know how to execute anonymous blocks of Apex and if you are new to the platform and just learning the ropes, then I hope this post helps you understand how you can play around with Apex without writing a single class.
What is Anonymous Apex? “An anonymous block is Apex code that does not get stored in the metadata, but that can be compiled and executed”, which simply means to execute Apex without having to first create class structures in order to test something out. Enter a few lines of code, hit execute and see the results instantly (not quite as sleek as Apple, but not far off).
So what kind of things can be done with Anonymous Apex?
1. Execute small snippets of Apex to test and learn how they work
2. Query and perform DML on records in the system – ad-hoc and dirty mass update is possible
3. Execute (Start Up) schedule or batch apex jobs
4. Test logic without creating records
5. Test Web Service call-outs
And where and how can Anonymous Apex be run:
1. Developer Console (more on that in a bit)
2. Workbench (alternative https://workbench.developerforce.com/login.php)
3. SublimeText with MavensMate (there will be no going back, once you do it here!)
4. Eclipse IDE (feeling Old Skool)
Let’s look at how we can execute blocks of Anonymous Apex in the Developer Console. Open the Developer Console from the setup menu and then bring up the Execute Anonymous Apex window by hit CRTL-E (CMD-E for MacOS) – should look like this:
The above example is the classic Print HelloWorld out to the screen (log)
String printHelloWorld = 'HelloWorld';
System.debug(printHelloWorld);
Something a little more interesting – let’s try some MATHS
Integer theNumberOne = 1;
Integer theNumberTwo = 2;
Integer result;
result = theNumberOne + theNumberTwo;
System.debug(result);
Often people get stuck trying to figure out how some of the methods which Salesforce provide work. This is perfect way for testing these out. Complex date manipulation can be figured out up front before writing any classes:
Date dtToday = Date.today();
Date dtFutureDate = dtToday.addDays(7);
System.debug('Today plus 7 days is: ' + dtFutureDate);
So what else can we do? What about creating or updating records in the system. Sometimes it’s useful to bypass the UI and create some records to shortcut the interface or where you don’t have a physical data source such as CSV which needs importing.
If you are familiar with creating records in Apex or are learning the ropes, you can create a single Account record with something like this;
Account acct = new Account(
name = 'Salesforce Playground'
);
insert acct;
System.debug('Just created this account' + acct);
*Important* Don’t forget to delete records if you are creating records for test purposes, here is the Apex to delete the above record
Account deleteMe = [SELECT Id FROM Account WHERE Name = 'Salesforce Playground' LIMIT 1];
delete deleteMe;
These are all very simple examples of course, designed to show you the basic sorts of things you can do. Now what about getting a feel for Class and object structures?
public class myClass {
public void showUsers() {
for (User u : [select username from User]) {
system.debug('nn#### ' + u.username + ' ####nn');
}
}
}
myClass c = new myClass();
c.showUsers();
These days I find myself playing around a lot with blocks of Apex snippets on pretty much every project, this is largely due to how easy MavensMate has made it to execute scripts quickly and repeatedly. Under MavensMate, anonymous apex is wrapped into something a little more meaningful than anonymous apex and given it name which matches the accessibility which they are providing here – “Apex Scripting”.
You can easily create multiple Apex scripts and execute them within a few clicks or keyboard shortcuts. This isn’t anything which couldn’t be done before, just something made more accessible. You very quickly build up a nice set of Apex Scripts which are quickly accessed and executed.
As always we would love to hear about what you have done within anonymous blocks of Apex, especially any hacks which have pushed the limits.
(*Important * please be sure to read what you can and cannot do within the context of Anonymous Apex here: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_anonymous_block.htm)