Salesforce Developers Blog

Hackathons, Batch Apex and a helping hand

Avatar for Quinton WallQuinton Wall
Thinking of adding some location services to you Hackathon app? Here is a handy little batch Apex class that might just do the trick to prep your data
Hackathons, Batch Apex and a helping hand
December 06, 2010
Listen to this article
0:00 / 0:00

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)
{
zips.add(a.BillingPostalCode);
}
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!

 

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS

More Blog Posts

Oleh Mykytyn Finds His Purpose as a Salesforce Developer

Oleh Mykytyn Finds His Purpose as a Salesforce Developer

Our Dev Spotlight series explores the lives of individual developers as they share their journey to becoming a Salesforce Developer and what it means to be a part of our worldwide community.August 24, 2023

Dreamforce ’23 Retrospective for Developers

Dreamforce ’23 Retrospective for Developers

A recap of the Salesforce innovations, announcements and content shared at Dreamforce 2023 for developers.September 28, 2023