Hackathons, Batch Apex and a helping hand

Movie Cloudstock is finally here. Yesterday was a buzz of activity as the final signage was raised, the lab machines were configured, and yes, the Hackathon is under way (don't worry, you can still start now if you haven't already!) with some amazing prizes for the winners (Kavindra and I were were at the Salesforce offices last night at around 10pm loading them into a cab!). 


But yesterday wasn't all work, and play. I dropped into the Thirsty Bear to say hi to some of the Force.com developer community members including Jeff Grosse, Brandy Colmer, and more.

In the spirit of the Hackathon and sharing I was thinking this morning about some ideas for people looking to build a rocking app using Force.com. One of the important aspects of Cloud 2 is the support for mobile, and location awareness (is that a word? Hey, it's 5am. Anything is a word at 5am if you ask me).  Could I combine sharing, and location?  I think so.

Sandeep helped me out the other day by sharing some code with me with a requirement for a demo (you will see it at Dreamforce, but I can't tell you any more that just yet!) which I wanted to share here in the hopes Hackathoners (another 5am word I know!) might be able to use it in their code.

My requirement was to populate records in Force.com with State codes based on a zips. We created a simple State_Zip_Mapping__c custom object to hold the mapping (you can Download the data here), then wrote a class which uses Batch Apex to loop through the records we want to update:


global class mapZip2State implements Database.Batchable{
public String query;
//Override start method of Database.Batchable interface
global Database.QueryLocator start(Database.BatchableContext BC){
String returnQuery = 'Select BillingPostalCode, BillingState from Account';
return Database.getQueryLocator(query);
global void execute(Database.BatchableContext BC, List accts){
Set zips = new Set();
for (Account a : accts)
Map zip2StateMap = new Map();
for (State_Zip_Mapping__c mapping : [select State__c, Zip__c from state_Zip_Mapping__c where zip__c in :zips])
zip2StateMap.put(mapping.Zip__c , mapping.State__c);
for (Account a : accts)
a.BillingState = zip2StateMap.get(a.BillingPostalCode);
update accts;
global void finish(Database.BatchableContext BC){

That's it. Now with a quick call to database.executeBatch from the console, I have just taken my Account records and mapped state codes to them.

If you end up using this code in the Hackathon, make sure you let us know. And of course, the bonus prize is who can the first person to correctly identify which movie the pic above came from? Yes, it's early I know……

Happy Coding!


tagged , , , , , , , , , , , , , , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • That would be from one of the GREAT Movie OS movies, Hackers. Hack the planet!

  • Quinton wall

    That was quick! Next time I have to make it way harder

  • Rich

    Custom settings might be a more optimal way to do the mapping.