1

Leads without any activity or an activity with no updates on Lead record should be moved to a Queue. We cannot achieve this through workflows, process builder, Assignment rules or a trigger. I would like to know if there is any way to implement this functionality which I may be overlooking/missing? Thank you!

2
  • a scheduled batch process??
    – Prady
    Mar 24, 2016 at 5:35
  • at what point in time do you decide it needs to be moved to a queue?
    – cropredy
    Mar 24, 2016 at 19:14

1 Answer 1

1

@ Soudam

Use Batch apex and schedule it to run at specific interval. I implemented this a while ago:

Batch Class:

public class OwnerReassignment implements Database.Batchable<sObject>{
    public String query;
    public String email;
    public Id qId;

    public Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext BC, List<sObject> scope){
        List<Lead> leads = new List<Lead>();
        for(sObject s : scope){
            Lead a = (Lead)s;
            a.OwnerId=qId;
            leads.add(a);
        }
        update leads;
    }
    public void finish(Database.BatchableContext BC){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Scheduler Class:

global class BatchScheduler implements Schedulable {
    global Database.Batchable<SObject> batchClass{get;set;}
    global Integer batchSize{get;set;} {batchSize = 10;}

    global void execute(SchedulableContext sc) {
    database.executebatch(batchClass, batchSize);
    }
}

You can instantiate this batch class from Developer Console:

OwnerReassignment reassign = new OwnerReassignment();

reassign.query = 'SELECT Id,LastModifiedDate,Status FROM Lead WHERE IsConverted = false AND LastModifiedDate < LAST_N_DAYS:30 AND LastActivityDate < LAST_N_DAYS:30 ';
reassign.email='PUT YOUR EMAIL ID';
reassign.qId='PUT QUEUE ID OR USER ID ';
BatchScheduler scheduler = new BatchScheduler();

// Assign the batch class to the variable within the scheduler
scheduler.batchClass = reassign;

// Run every day at 12pm
String sch = '0 0 12 1/1 * ? *';
System.schedule('LDN Leads Reassigment', sch, scheduler);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .