/ Published in: Java
                    
                                        
Batch Apex class that allows you to reassign leads based on ActivityHistory.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
global class LeadReassignOnActivityHistory implements Database.Batchable<sObject>{
global Iterable<sObject> start(Database.BatchableContext BC) {
// This set will hold the Id's of the Leads that we will reassign.
// Query all the Leads in your database and sub-query all existing Activities that match your criteria (i.e. Type), and return the newest one.
// Iterate through the list of leads returned above.
for (Lead l : startLeads) {
// if the Lead does not have any Activities associated with it then reassign it.
if (l.ActivityHistories.size() == 0 || l.ActivityHistories.size() == null) {
LeadIds.add(l.Id);
}
//for each lead, iterate through its related Activities
for (ActivityHistory activity : l.ActivityHistories) {
// if the Activity is older than the reassignDate variable you set above, then this lead's ID will be added to the list that will be reassigned.
if (activity.ActivityDate < reassignDate) {
LeadIds.add(activity.WhoId);
}
} // close inner for-loop
} //close outer for-loop
// Using the list of ID's above, query the database for all related leads and get the necessary fields.
// Return this list of leads, which will get passed to our execute() method below.
return leadsToReassign;
} //close start method
// This list of tasks will be used to hold tasks for insert.
// Iterate through the scope of Leads that were passed through to the execute method and transfer ownership based on the location.
// This example only has two locations.
// Create a Task that's associated with each Lead, this can be used so that we don't reassign the same Lead every day if no activity is being logged.
for(Lead l : scope) {
if(l.Location__c == 'Location A') {
l.OwnerId = '005A0000000i84o'; // reassigns ownership via hardcoded Id
Task tsk = new Task(); // Create the new task and populate it with data.
tsk.WhoId = l.Id;
tsk.Status = 'Completed';
tsk.Subject = 'Ownership Transferred';
tsk.Type = 'Other';
taskList.add(tsk); // add the task to the list.
} //close if statement
else {
l.OwnerId='005A0000000i84z';
Task tsk = new Task();
tsk.WhoId = l.Id;
tsk.Status = 'Completed';
tsk.Subject = 'Ownership Transferred';
tsk.Type='Other';
taskList.add(tsk);
} //close else
} //close for-loop
try {
insert taskList;
} catch (system.dmlexception e) {
}
try {
update scope;
} catch (system.dmlexception e) {
}
} //close execute method
global void finish(Database.BatchableContext BC) {
AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
from AsyncApexJob where Id =
:BC.getJobId()];
// Create and send an email with the results of the batch.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayName('Lead Reassignment Results');
mail.setSubject('Lead Reassignment ' + a.Status);
mail.setPlainTextBody('The batch apex job processed ' + a.TotalJobItems +
' batches with ' + a.NumberofErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
} //close finish method
} //close class
Comments
 Subscribe to comments
                    Subscribe to comments
                
                