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.
global classScheduleUpdateContacts implementsSchedulable {
2
global voidexecute(SchedulableContext SC) {
3
UpdateAllContacts uac = newUpdateAllContacts();
4
database.executebatch(uac);
5
} //close execute method
6
} //close class
Test Class that Achieves 94% Code Coverage:
01
@isTest
02
privateclassUpdateAllContactsTest {
03
04
statictestMethod voidTestUpdateAllContacts() {
05
06
// User Id's for the two sales managers.
07
String aId = 'XXXXXXXXXXXXXXXX';
08
String bId = 'XXXXXXXXXXXXXXXX';
09
10
List <Contact> contacts = newList <Contact>();
11
List <Task> tasks = newList <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 = newContact(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 = newContact(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 = newTask();
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 = newUpdateAllContacts();
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.
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.
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.
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...
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