The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Force.com Anonymous Blocks

with one comment

Anonymous Blocks. Where have you been all my life? Right under my nose apparently. I am embarrassed to say I’ve only discovered this little jewel of the Force.com platform recently, and although I’ve managed without it, I’m pretty sure that it’ll be a regular feature in my development experience.

What are they you ask? It’s simply the ability to execute a bit of Apex code on the fly. This is awesome^6 at least. Often during the development process you need to clean your data. Either you’ve changed your logic and that data is affecting functionality, or perhaps you need to remove data en masse. This is simple with Anonymous Blocks, just write up a short script that will restore your data to an ‘initial’ state and save it for future use. Any time things get messy just run your script and life is good again. The scripts would be written in  Apex code without the need for the enclosing class structure.

As an example let’s consider the following situation:

  • You have two custom objects, A__c and B__c.
  • A__c and B__c are related by a master-detail relationship.
  • A__c has a boolean field processed__c that defaults to false.
  • A workflow is defined such that if a certain thing happens to A__c, a child relationship of object type B__c must be created, and the processed__c field on A__c must be set to true.

So you do you thing for a week, developing you app and creating data all along the way. But now your dev scope calls for some other functionality and you need to develop an after insert trigger on object type B__c. Alas alack! The new logic doesn’t cater for the current system data state and your app is being plain old weird. Not to worry, Anonymous Blocks to the rescue! All you need to do is delete all records of object type B__c and update A__c.processed__c to false.

List<B__c> bees = [SELECT id FROM B__c];

delete bees;

List<A__c> ayes = [SELECT processed__c FROM A__c];

List<A__c> ayesToUpdate = new List<A__c>();

for(A__c a: ayes){

a.process__c = false;

ayesToUpdate.add(a);

}

update a;

And that’s it. No need to put it into an Apex class, just run the sucker.

[Aside: Oh yes, you can also use Anonymous Blocks to test bits of code, but I’d rather use test classes for that]

I know what you’re thinking, ‘Wes this sounds smashing. But how do I execute an Anonymous Block?’. Well think no more my friend. There are three ways

1. The in-browser System Log

systemlog

2.  The Force.com IDE

IDE

3. The executeAnonymous Force.com Web Services API call

ExecuteAnonymousResult executeAnonymous(String code)

Considerations

There are a number of considerations when working with Anonymous Blocks, the only one I encounter often being You cannot reference variable before their declaration within the code i.e. if a variable is declared at line 40, a method declared before this line cannot reference said variable.

This tiny area of the platform is massively helpful and I’m sure I’ll find many more uses in the near future. If anyone out there has used this tool in any marvelous way I’ve not mentioned, feel free to drop me a line.

Written by Wes

June 27, 2009 at 8:14 pm

One Response

Subscribe to comments with RSS.

  1. […] this piece of code(perhaps as an anonymous block) would give the following, Value in: […]


Leave a comment