Leverage Cloud-Computing, Saas, and Better Business Processes to Drive Change in Your Industry

Clint Lee

Subscribe to Clint Lee: eMailAlertsEmail Alerts
Get Clint Lee: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: Salesforce.com Journal

Salesforce Journal: Blog Post

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>{
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';
07 global database.queryLocator start(Database.BatchableContext BC) {
08 return database.getQueryLocator(query);
10 } //close start method
12 global void execute(Database.BatchableContext BC, list <Contact> scope) {
14 List <Task> taskList = new List<Task>();
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') {
22 Task tsk = new Task();
23 tsk.WhoId = c.Id;
24 tsk.ActivityDate = System.today();
25 tsk.Status = 'Completed';
26 tsk.Subject = 'Ownership Transferred';
28 taskList.add(tsk);
30 } //close if statement
31 else {
33 Task tsk = new Task();
34 tsk.WhoId = c.Id;
35 tsk.ActivityDate = System.today();
36 tsk.Status = 'Completed';
37 tsk.Subject = 'Ownership Transferred';
39 taskList.add(tsk);
40 } //close else
41 } //close for-loop
43 try {
44 insert taskList;
45 } catch (system.dmlexception e) {
46 System.debug('Tasks not inserted: ' + e);
47 }
49 try {
50 update scope;
51 } catch (system.dmlexception e) {
52 System.debug('Scope not updated: ' + e);
53 }
55 } //close execute method
57 global void finish(Database.BatchableContext BC) {
59 AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
60 TotalJobItems, CreatedBy.Email
61 from AsyncApexJob where Id =
62 :BC.getJobId()];
64 // Create and send an email with the results of the batch.
65 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
67 mail.setToAddresses(new String[] {a.CreatedBy.Email});
68 mail.setReplyTo('[email protected]');
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 });
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 {
04 static testMethod void TestUpdateAllContacts() {
06 // User Id's for the two sales managers.
10 List <Contact> contacts = new List <Contact>();
11 List <Task> tasks = new List <Task>();
13 Test.StartTest();
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);
23 } //close for-loop
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);
33 } //close for-loop
35 insert contacts;
37 List <Contact> cont = [Select ID, FirstName from Contact Where FirstName=:'Test' limit 200];
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';
48 tasks.add(tsk);
50 } // close for-loop
52 try {
53 insert tasks;
54 } catch (System.DMLexception e) {
55 System.debug('Task List not inserted: ' + e);
56 }
58 // Call the Batch Apex method.
59 UpdateAllContacts uac = new UpdateAllContacts();
60 ID batchprocessid = Database.executeBatch(uac);
61 Test.StopTest();
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);
71 } //close testmethod
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.

More Stories By 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.