Untitled Document
 Register Now & Save!
Untitled Document
2009 Gold Sponsor
Untitled Document
2009 Silver Sponsor
Untitled Document
2009 Panel Sponsor
Untitled Document
2009 Exhibitors
Untitled Document
2009 Media Sponsors
Latest News
In 2011, Apache Hadoop received tremendous attenti...
AMD said late Tuesday that its chief sales officer...
Intel has finally seen the back of that 2009 antit...
On Tuesday, Clustrix announced the availability of...
What are the legal implications and consequences o...
EMC moved to make Hadoop safe for the Joe Blow big...
Amazon has reined in the price of its S3 storage a...
The focus of Java EE 7 is on the cloud, and specif...
2011 was a year of rapid adoption for public and p...
AMD Thursday told financial analysts it’s gonna tr...
Can't Miss RSS Feed
Subscribe to the RSS Feed & Get All The Conference News As It Happens!
A Business Use Case for Batch Apex
With Code and Test Class

I want to share a recent real-world use case where Batch Apex was used to achieve our goal.  If you’re not familiar with Batch Apex you can find more here in the Salesforce Development Docs.

Business Use Case
An organization’s sales team is using Salesforce CRM primarily to manage their Leads, Contacts, and opportunities. This is akin to a retail setting whereby there are multiple physical locations and each location has its own sales team and sales manager (think car dealership).  In this setting, Leads (and/or Contacts) enter through multiple channels such as internet marketing, phone calls, walking through the front door, etc and are assigned to a sales rep based on a round-robin format.  That sales rep becomes the owner of the Lead, but only has protection for a certain period of time before the Lead is redistributed.  The protected period of time is based on the sales rep’s last activity to the Lead/Contact.  In other words, if the rep is not actively working the Lead/Contact then they lose protection, and if that Lead/Contact ends up buying a product then the rep will not be credited for the commission.

The question then became, how can we automate this process of removing Lead protection based on the sales rep’s activity?  One of the great features of working with the Force.com platform is that there are always multiple ways to solve these types of problems.  And these are good problems to solve because the solutions free up people’s time and help advance business.  In this situation, the basic design for this scenario is that when the number of days since the last activity is greater than 5 days, revert the ownership of the Lead/Contact from the sales rep to the sales manager.  This lets the sales manager redistribute the Lead/Contact to another sales rep.

Make It Happen
The first step was to create a new formula field that returns a Number and call it Days_Since_ Last_Activity.  The formula is this: Today() – LastActivityDate.  At this point, you might think about creating a Workflow Rule that says when Days_Since_Last_Activity is greater or equal to 5 then Update the Owner field.  However, a Workflow Rule is only fired under three circumstances: a)Only when a record is created; b) Every time a record is created or edited or; c) When a record is edited and did not previously meet the criteria.  This means that the workflow rule won’t get fired until the record gets edited, and we want the ownership to change immediately upon the passing of the protected period regardless of whether or not someone edits the record.

The solution in this case is to use Batch Apex to query the database for all Leads/Contacts (we use Contacts in the example code) that have crossed the protected period, i.e. the Days_Since_Last_Activity fields is greater than 5.  Then, we reassign ownership to the correct sales manager based on which physical location this Contact is associated with.  Next, we create a new Task associated with this Contact so that the Days_Since_Last_Activity gets reset.  Lastly, we schedule this Batch Apex to run each night so that the ownership is being recalculated on a daily basis.

Below are the Apex and Test Classes that make this work.


Batch Apex Class:

01 global class UpdateAllContacts implements Database.Batchable<sObject>{
02
03 //This is the query that is passed to the execute method.  It queries all of the Contacts who have passed
04 //the protected period.
05 String query = 'Select Id, Club_Location__C, OwnerId FROM Contact WHERE Days_Since_Last_Activity__c > 5';
06
07 global database.queryLocator start(Database.BatchableContext BC) {
08 return database.getQueryLocator(query);
09
10 } //close start method
11
12 global void execute(Database.BatchableContext BC, list <Contact> scope) {
13
14 List <Task> taskList = new List<Task>();
15
16 // Iterate through the whole query of Contacts and transfer ownership based on the location.
17 // This example only has two locations.
18 // Create a Task that's associated with each Contact.  This resets the Days Since Last Activity formula field.
19 for(Contact c : scope) {
20 if(c.Location__c == 'Location 1') {
21 c.OwnerId = 'XXXXXXXXXXXXXXXX';
22 Task tsk = new Task();
23 tsk.WhoId = c.Id;
24 tsk.ActivityDate = System.today();
25 tsk.Status = 'Completed';
26 tsk.Subject = 'Ownership Transferred';
27
28 taskList.add(tsk);
29
30 } //close if statement
31 else {
32 c.OwnerId='XXXXXXXXXXXXXXXX';
33 Task tsk = new Task();
34 tsk.WhoId = c.Id;
35 tsk.ActivityDate = System.today();
36 tsk.Status = 'Completed';
37 tsk.Subject = 'Ownership Transferred';
38
39 taskList.add(tsk);
40 } //close else
41 } //close for-loop
42
43 try {
44 insert taskList;
45 } catch (system.dmlexception e) {
46 System.debug('Tasks not inserted: ' + e);
47 }
48
49 try {
50 update scope;
51 } catch (system.dmlexception e) {
52 System.debug('Scope not updated: ' + e);
53 }
54
55 } //close execute method
56
57 global void finish(Database.BatchableContext BC) {
58
59 AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
60 TotalJobItems, CreatedBy.Email
61 from AsyncApexJob where Id =
62 :BC.getJobId()];
63
64 // Create and send an email with the results of the batch.
65 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
66
67 mail.setToAddresses(new String[] {a.CreatedBy.Email});
68 mail.setReplyTo('batch@mycompany.com');
69 mail.setSenderDisplayName('Batch Processing');
70 mail.setSubject('Contact Update ' + a.Status);
71 mail.setPlainTextBody('The batch apex job processed ' + a.TotalJobItems +
72 ' batches with ' + a.NumberofErrors + ' failures.');
73
74 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
75
76 } //close finish method
77 } //close class


Apex Class Used to Schedule the Batch Apex Class:

1 global class ScheduleUpdateContacts implements Schedulable {
2 global void execute(SchedulableContext SC) {
3 UpdateAllContacts uac = new UpdateAllContacts();
4 database.executebatch(uac);
5 } //close execute method
6 } //close class


Test Class that Achieves 94% Code Coverage:


01 @isTest
02 private class UpdateAllContactsTest {
03
04 static testMethod void TestUpdateAllContacts() {
05
06 // User Id's for the two sales managers.
07 String aId = 'XXXXXXXXXXXXXXXX';
08 String bId = 'XXXXXXXXXXXXXXXX';
09
10 List <Contact> contacts = new List <Contact>();
11 List <Task> tasks = new List <Task>();
12
13 Test.StartTest();
14
15 // Create 50 Contacts and assign them to the sales manager of the opposite location.
16 for (integer i=0; i<50; i++) {
17 Contact c = new Contact(FirstName='Test',
18 LastName='Contact'+ i,
19 Location__c = 'Location 1',
20 OwnerId = bId);
21 contacts.add(c);
22
23 } //close for-loop
24
25 // Create 50 more Contacts and assign them to the sales manager of the opposite location.
26 for (integer i=0; i<50; i++) {
27 Contact c = new Contact(FirstName='Test',
28 LastName='Contact' + i + i,
29 Location__c = 'Location 2',
30 OwnerId = aId);
31 contacts.add(c);
32
33 } //close for-loop
34
35 insert contacts;
36
37 List <Contact> cont = [Select ID, FirstName from Contact Where FirstName=:'Test' limit 200];
38
39 // Create a Task for each Contact that was just inserted.  Set the date of the task so that it will set the
40 // Last Activity Date to a date older than your protection period.
41 for (Integer i=0; i<100; i++) {
42 Task tsk = new Task();
43 tsk.WhoId = cont.get(i).Id;
44 tsk.ActivityDate = System.today() - 15;
45 tsk.Status = 'Completed';
46 tsk.Subject = 'Test Subject';
47
48 tasks.add(tsk);
49
50 } // close for-loop
51
52 try {
53 insert tasks;
54 } catch (System.DMLexception e) {
55 System.debug('Task List not inserted: ' + e);
56 }
57
58 // Call the Batch Apex method.
59 UpdateAllContacts uac = new UpdateAllContacts();
60 ID batchprocessid = Database.executeBatch(uac);
61 Test.StopTest();
62
63 AsyncApexJob async = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems from AsyncApexJob where Id = :batchprocessid];
64 System.debug('Final results are ' + async);
65
66 System.AssertEquals(async.NumberOfErrors, 0);
67 System.AssertEquals([Select count() from Contact Where OwnerId=:aId AND FirstName='Test'], 50);
68 System.AssertEquals([Select count() from Contact Where OwnerId=:bId AND FirstName='Test'], 50);
69 System.AssertEquals([Select count() from Task Where Subject = 'Test Subject'], 100);
70
71 } //close testmethod
72
73 } //close Class

 

Once you’ve successfully saved your Apex Classes go to Setup –> Develop –> Apex Classes –> Schedule Apex. Use the class above that implements the Schedulable interface and select the frequency that you want the class to run.

Hope this is helpful. As always, I look forward to your thoughts and/or feedback.

About Clint Lee
Clint is a Principal and Founder of The Flywheel Group, a premier provider of innovative solutions to the franchise industry. The FranchiseFlywheel™ application was developed out of the industry's need for a cutting-edge and affordable franchise management tool for franchisors. Spending several years in the franchise industry directing daily activities related to franchise and business development processes, marketing, lead-generation, and contract administration, and an understanding of the needs of peers and colleagues, led to the development of the application. The Flywheel Group is focused on improving best practices and driving change in the franchise industry by introducing industry-leading solutions and working with its clients to re-engineer business processes that will ultimately enhance revenue and improve bottom line efficiency. Clint actively writes and shares his thoughts on the Flywheel Blog.

Untitled Document

Call 201 802-3021 or Click Here to Save $400!

Save $400

 Sponsorship Opportunities

SYS-CON's International Cloud Computing Conference & Expo, held each year in California, New York and Prague is the leading event covering the fast-emerging Cloud Computing market for Enterprise IT professionals. Co-located with the International Virtualization Conference & Expo, the combined event will surely deliver the #1 i-Technology educational and networking opportunity of the year for those seeking to establish a market lead anywhere in the multiple layers of the Cloud Computing ecosystem.





Who Should Attend?

Senior Technologists including CIOs, CTOs, VPs of technology, IT directors and managers, network and storage managers, network engineers, enterprise architects, communications and networking specialists, directors of infrastructure Business Executives including CEOs, CMOs, CIOs, presidents, VPs, directors, business development; product and purchasing managers.


Video Coverage of Cloud Computing Expo

Brian Stevens: The Opening of Virtualization
Jon Wallace: User Environment Management – The Third Layer of the Desktop
Brian Duckering & Ken Berryman: Managing Hybrid Endpoint Environments
Preeti Somal: Game-Changing Technology for Enterprise Cloud and Applications

 Conference Media Sponsor: Cloud Computing Journal

Cloud Computing Journal aims to help open the eyes of Enterprise IT professionals to the economics and strategies that utility/cloud computing provides. Cloud computing - the provision of scalable IT resources as a service, using Internet technologies - potentially impacts every aspect of how IT deploys and operates software.

Government IT Conference & Expo 2009
Allstar Conference Faculty Lineup Will Include...


CHEVALIER

Novell Canada

DICARLO

Sun Micosystems

FOXWELL

Sun Microsystems Federal

GABHART

Web Age Solutions

GREENBERG

Integralis

HAHN

Tranxition

WILLIAMS

Maxworks

JACKSON

Dataline, LLC

KHOSLA

IBM

KRZYSKO

US Departement of Defense

LIBERMAN

Lieberman Software

MARKS

AgilePath

MORGENTHAL

QinetiQ North America

RYAN

Asankya

TRAJMAN

Vertica

WHITE

BDNA


SYS-CON EVENTS


Past Events Archive

Cloud Computing Conference & Expo
2009 East

cloudcomputingexpo
2009east.sys-con.com/
Virtualization Conference & Expo
2009 East

virtualizationconference
2009east.sys-con.com/
Cloud Computing Conference & Expo
2008 West

cloudcomputingexpo
2008west.sys-con.com/
SOAWorld Conference & Expo 2008 West
soaworld2008.com/
Virtualization Conference & Expo 2008 West
virtualizationconference
2008west.sys-con.com
AJAXWorld Conference & Expo 2008 West
ajaxoct08.sys-con.com
SOAWorld Conference & Expo 2008 East
soa2008east.sys-con.com
Virtualization Conference & Expo 2008 East
virt2008east.sys-con.com
AJAXWorld 2008 Conference & Expo East
ajaxmar08.sys-con.com
SOAWorld Conference & Expo 2007 West
www.soaworld2007.com
Virtualization Conference & Expo 2007 West
virt2007west.sys-con.com
AJAXWorld 2007 Conference & Expo West
ajaxoct07.sys-con.com

Cloud Computing Expo Alumni Delegates Represents...

• AccuRev
• Adea Solutions
• Adobe Systems, Inc [3 delegates]
• ADP
• Aeropostale, Inc
• Aetna
• Akbank Training Center
• American Family Insurance
• American International College
• American Modern Insurance
• Amphion Innovations
• Amplify LLC, Clipmarks [2 delegates]
• Anderson Consulting
• Arrow Electronics [3 delegates]
• Ashcroft Inc
• Athabasca University
• ATS
• Audatex
• Avanade, Inc.
• Avaya Inc. [5 delegates]
• Azul [2 delegates]
• Backbase [2 delegates]
• Bank of America
• Bank of NY
• Barnes and Noble
• Barnex Investment International Limited
• BEA
• Bear Stearns [2 delegates]
• Bendel Newspaper Company Limited
• BizInnovative
• Bloomberg [2 delegates]
• BlueBrick Inc.
• BMC Software
• Boeing
• Bottomline Technologies [2 delegates]
• BP
• Broadcom

   read more...
Cloud Computing Blogs
In other words, VMware’s server density is higher. Boles suggests this means that customers should be “assessing virtualisation on a ‘cost per application’ basis. VM density has a sign
Traditionally, the way people have implemented high availability is by using a high-availability management package like Linux-HA[1], then configure it in detail for each application, file system moun