Apex is revolutionary
I can’t stop giggling. My first 25-line Apex coding project will replace a 400+ line S-Control. Seriously, it’s blowing my mind a bit.
Here’s the code:
trigger HouseholdCheck on Contact bulk (before insert) {
for (Contact c : Trigger.new) {
if (c.ONEN_Household__c == NULL) {
String RecognitionName;
RecognitionName = c.firstname + ' ' + c.lastname;
ONEN_Household__c hh = new ONEN_Household__c (
Name=RecognitionName + ' Household',
Recognition_Name__c=RecognitionName,
Recognition_Name_Short__c=c.firstname,
MailingStreet__c=c.MailingStreet,
MailingCity__c=c.MailingCity,
MailingState__c=c.MailingState,
MailingPostalCode__c=c.MailingPostalCode,
MailingCountry__c=c.MailingCountry
);
insert hh;
c.ONEN_Household__c = hh.id;
}
if (c.AccountId == NULL) {
Account[] account = [select id from account where Name = 'Individual'];
c.AccountId = account[0].Id;
}
}
}
I won’t paste in the 400 line HTML form and code that it’s replacing…
All the new code does is create a Household for a Contact when you’re creating a Contact. If you’ve selected a Household, it doesn’t create one. It also adds the Contact to the Individual account if you haven’t picked a different one.
Simple, huh?
But it’s been really hard to do in Salesforce.com before Apex. We had to build a custom Contact creation form that was seriously 400+ lines long.
Not only does it replace my custom data entry screen, it fixes all sorts of problems. We couldn’t use Quick Create because a Household wouldn’t be created when the Contact was–now it will. We couldn’t let people sync Outlook contacts for the same reason–now we can. When a Contact is created anywhere, anyway, by anyone, the trigger will fire, and they will have a Household.
That also makes the integration story better. Our Plone integration won’t have to care about the Individual Account. It won’t have to worry about Households. We’ll have an Apex trigger like this one that will take care of our crazy business rules behind the scenes.
I knew Apex was coming and that it would make this kind of thing possible. But actually learning enough of the language to write this code in under two hours was more than I could have hoped for.
Apex is revolutionary. These guys are definitely on to something.

March 29th, 2007 at 8:56 pm
Ah, and so it begins. Looking forward to some discussions over at the NTC next week to see how we can crank out some effeciency gains on the Joomla side as well. Any time you read a 90% reduction in code, you know that something very powerful is going on. Glad to see the community’s got someone like you kicking the tires.
Rock on!
-Ryan Ozimek
March 29th, 2007 at 9:38 pm
Yeah I’ll do a quick demo for you next week. And you’ll see how elegant it is. Really slick stuff!
March 30th, 2007 at 1:40 am
That is so great to read. Can’t wait to see more examples!
March 30th, 2007 at 6:56 am
My “Blow your mind” moment came when I realized that I could take use 2 lines of code to turn an APEX Code method to a WSDL that could be accessed via AJAX and could clean up a huge amount of ugly JavaScript code.
-GlennW
March 30th, 2007 at 7:02 am
Yeah Glenn, I can’t wait to try that. Working with the AJAX toolkit has been really great, but I’m looking forward to moving complex logic down to Apex where it will be faster and more maintainable. I really like the fact that it’s compiled on save time so the Eclipse plug-in pointed out my errors to me without having to do the Save-Run in Browser-Look at Firebug for the Error-Fix-Save-Run in Browser loop!
March 30th, 2007 at 9:15 am
love to hear you’re giggling. more o that!
blessings, jodie
March 31st, 2007 at 9:16 am
Steve - that’s great news. I’ll see you at NTEN, yes? My only complaint is that your work is moving faster than I am - just when I start to tell the nonprofit community about something new and cool - you’ve made it newer and cooler!
pcs
March 31st, 2007 at 9:36 am
Yes, I’ll see you there! And remeber, it’s not me that gets newer and cooler, it’s Salesforce.com that gets newer and cooler. I get older and less cool each day!