As A Service

Developing on Salesforce

Apex Code for Beginners at the Salesforce.com Spring ’07 release

The final session that I attended at the launch was Apex Code for Beginners, delivered by Adam Gross. It was a great talk, providing some nice slices of Apex Code to whet your appetite.

Adam sees the Apex Platform as based on five pillars:

  • Data & Native Framework – the full-stack on-demand framework for creating apps. (I think I can see why they call it a “native framework” but that name just confuses me.)
  • The API – that’s actually the Web services API they refer to when they say “API” It’s now running at over 50% of the traffic, though from what I read that may go down if folk started using the relationship queries that were recently introduced.
  • Packaging and Distribution – does what it says on the tin
  • Apex Code – their multi-tenant programming language
  • Composite Framework – another interesting name. I think what they mean here is Extensibility/Integration Framework

Adam was really keen that folk starting using all the new features, including Apex Code. Of course, you can’t use them in production yet – but they’re there now for us to provide some feedback and hopefully drive the future direction. Starting to use them now will also give us a little advantage when the rest of the world realizes how important they are ;-)

So, Apex Code. This language is exclusively for business logic. I like the analogy Adam made. ‘”Conceptually” it’s similar to a stored procedure’ – that’s the analogy I had in my head when I started using Apex Code. The important thing to remember is that it’s not a general purpose programming language – you can’t go and write a UI. It’s specifically designed for data manipulation and business logic.

A little example (not quite what Adam used – this is from a whitepaper on ADN):


01. // Define a trigger for the Lead object, and set it to
02. // fire on the insert and update events
03. trigger leadDupCheck on Lead(before insert, before update) {
04. 	// Make sure the email and zip code fields aren’t null
05. 	if (Trigger.new.Email != null && 
                             Trigger.new.PostalCode != null ) { 
06.      // Query for all leads that have the same email and zip code
07.      // as the new lead 
08.      Integer dupCount = 
09.          [select count() from Lead WHERE email = :Trigger.new.Email 
10.	          AND PostalCode = :Trigger.new.PostalCode ];
11.      if ( dupCount > 0 ) { 
12.       // Lead exists, so display an error message
13.       Trigger.new.email.addError('Lead is a duplicate');
14.      } else { 
15.        // No dup is found, so let the processing continue
16.      }
17.  }  	
18. }

I think it’s pretty readable, but here are some notes:

  • This code triggers before a Lead object is inserted or updated. See Line 03. Note that the code will run irrespective of how the Lead object is inserted or updated. For example, through the web services API or through the UI.
  • Line 05 accesses Trigger.new.Email – this actually refers to the Email field in the incoming Trigger object, in this case the Lead object being created/updated. So in effect your trigger code has full access to the object during the period after someone started its “creation” but before it’s been “persisted” – this is awesome of course, and opens up a number of opportunities.
  • Check out the embedded, inlined SOQL query on line 9. It automatically grabs the Email and PostalCode fields from the incoming Lead object too. Neat.

Other Apex features Adam noted: strongly typed, fine grained event model (insert, update, delete), transaction control, native web services production, App Exchange friendly (packageable and redistributable). Also, the code is governed. Because it’s multi-tennant, you don’t really want one client running code that slows down other clients, so they’ve added some governors. In essence, it sounds like the runtime observes the execution, tallies up some things, and if they exceed some limits throws an exception instead of letting your code hog the machine. Some of the possible constraints Adam mentioned were: total number of SOQL statements executed, total number of rows retrieved, data modifications and transactions. I’d be interested to see how these governors work in the case of a bulk input for example.

A question that popped into my mind is “how do I test this.” I’d really like a way of setting up test cases, test data and execution runs of my triggers :-)

Adam also pointed to the Eclipse Toolkit (in fact, he used it) for remove validation/compilation (neat). He also mentioned something about “AppLab” – training for Apex Code in London happening April 16. Unfortunately I can’t find any details on the developer site yet – so if anyone from Salesforce is reading, please leave a comment!

All in all this was a great session where I learned a lot. I expect a lot more developers will start to use Apex Code once they realize how powerful it is—and how it provides features not otherwise available.

Adam spoke about how they were looking for feedback, so in that spirit:
I find the Apex Code syntax a little weird in a couple of places, which may throw a lot of folk new to the language:

  • They use things that look remarkably like arguments, to carry metadata instead. Ack! So the “arguments” on Line 3 aren’t actually arguments to the trigger, but rather they’re metadata denoting when the trigger should be run.
  • I wish they let me name the incoming parameter, the actual trigger object. Right now it’s just “Trigger”
  • Metadata is hot. Everyone is using it. Check out EJB 3 for the beauty of metadata. Once Apex Code takes off they’re going to want to introduce even more metadata. How about following Java’s lead on this and use something extensible?

Putting all of this together, I get:


/*
 * @Trigger.beforeUpdate
 * @Trigger.beforeInsert
 */
 trigger leadDupCheck (Lead incoming) {
   if (incoming.Email != null) ...  
 }

Technorati Tags: ,

Advertisements

22 March 2007 - Posted by | Apex Code, Salesforce

4 Comments »

  1. Hi Jon,

    Drop me a line sounds like you’re in London. We should try to catch up.

    Cheers
    Gareth.

    Comment by Gareth Davies | 22 March 2007 | Reply

  2. This is a great summary. It really will helps many of on new developers interested in taking advantage of the Apex Platform.

    I am happy to hear your positive experiece at the Spring ’07 launch.

    What were some of your take away points and what is your message to the developer community with regards to their future with “On-Demand” platforms and specifically the Apex Platfrom.

    Comment by Kavindra | 22 March 2007 | Reply

  3. One more thing. The “AppLab” class will soon go on the ADN home page. For now, use this link to find out more.

    Cheers! -Kavindra

    Comment by Kavindra | 22 March 2007 | Reply

  4. This is much relevant to skill enhancing

    Comment by mohit k sharma | 22 April 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: