From the Blogosphere
A Business Use Case for Batch Apex
With Code and Test Class
Jul. 26, 2010 02:55 PM
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>{ |
05 |
String query = 'Select Id, Club_Location__C, OwnerId FROM Contact WHERE Days_Since_Last_Activity__c > 5'; |
07 |
global database.queryLocator start(Database.BatchableContext BC) { |
08 |
return database.getQueryLocator(query); |
12 |
global void execute(Database.BatchableContext BC, list <Contact> scope) { |
14 |
List <Task> taskList = new List<Task>(); |
19 |
for(Contact c : scope) { |
20 |
if(c.Location__c == 'Location 1') { |
21 |
c.OwnerId = 'XXXXXXXXXXXXXXXX'; |
22 |
Task tsk = new Task(); |
24 |
tsk.ActivityDate = System.today(); |
25 |
tsk.Status = 'Completed'; |
26 |
tsk.Subject = 'Ownership Transferred'; |
32 |
c.OwnerId='XXXXXXXXXXXXXXXX'; |
33 |
Task tsk = new Task(); |
35 |
tsk.ActivityDate = System.today(); |
36 |
tsk.Status = 'Completed'; |
37 |
tsk.Subject = 'Ownership Transferred'; |
45 |
} catch (system.dmlexception e) { |
46 |
System.debug('Tasks not inserted: ' + e); |
51 |
} catch (system.dmlexception e) { |
52 |
System.debug('Scope not updated: ' + e); |
57 |
global void finish(Database.BatchableContext BC) { |
59 |
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, |
60 |
TotalJobItems, CreatedBy.Email |
61 |
from AsyncApexJob where Id = |
65 |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); |
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.'); |
74 |
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); |
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); |
Test Class that Achieves 94% Code Coverage:
02 |
private class UpdateAllContactsTest { |
04 |
static testMethod void TestUpdateAllContacts() { |
07 |
String aId = 'XXXXXXXXXXXXXXXX'; |
08 |
String bId = 'XXXXXXXXXXXXXXXX'; |
10 |
List <Contact> contacts = new List <Contact>(); |
11 |
List <Task> tasks = new List <Task>(); |
16 |
for (integer i=0; i<50; i++) { |
17 |
Contact c = new Contact(FirstName='Test', |
18 |
LastName='Contact'+ i, |
19 |
Location__c = 'Location 1', |
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', |
37 |
List <Contact> cont = [Select ID, FirstName from Contact Where FirstName=:'Test' limit 200]; |
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'; |
54 |
} catch (System.DMLexception e) { |
55 |
System.debug('Task List not inserted: ' + e); |
59 |
UpdateAllContacts uac = new UpdateAllContacts(); |
60 |
ID batchprocessid = Database.executeBatch(uac); |
63 |
AsyncApexJob async = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems from AsyncApexJob where Id = :batchprocessid]; |
64 |
System.debug('Final results are ' + async); |
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); |
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 LeeClint 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.