function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mayank.msMayank.ms 

Batchable class to update the lead status when no activity logged in within 30 days.

Hi All,

I need a Batchable class that will update the lead Status as 'Open' when no actvities logged under Lead Object

Step1: We have 2 lead status 'Contacted Attempt Made' and 'Discovery'. When these status set, I update the stage date (Custom field Stage_date__c). 
Step2: If stage date is more than 30 days and no actvity logged under lead object from stage date then we need to update the Lead status as 'Open'

Can you please provide the class to update the stage 

Thanks in advanced
 

.

 

 

Best Answer chosen by Mayank.ms
BALAJI CHBALAJI CH
Hi Kamran,
Can you please below code,
 
global class LeadLifeCycleWorkflow implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Stage_days__c,Stage_date__c,Status,OwnerId, (select id, createdDate,status from Tasks ) FROM Lead WHERE Status in (\'Contacted Attempt Made\', \'Discovery\') and Stage_days__c>29' ;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        List<Lead> listLead = new List<Lead>();
        for(Lead l : scope)
        {
            if(l.Stage_days__c==30 && l.Tasks.size()==0)
            {
                for(Task t : l.Tasks)
                {
                    if(t.createddate > l.Stage_date__c)
                    {
                        l.Status='Open';
                        listLead.add(l);
                    }
                }
            }  
        } 
        if(!listLead.IsEmpty()){
            update listLead;
        } 
    }
    
    global void finish(Database.BatchableContext BC)
    {
        
    }
}

Let us know if that works for you.

Best Regards,
BALAJI

All Answers

GauravGargGauravGarg
Hi Kamran,

I will guide you, how you can achieve this:
  1. Create a scheduler on daily basis. 
  2. Call a batch class from the Scheduler and set size of batch. 
  3. Query all the records which are in your condition, i.e. no activity logged for 30 days. 
  4. In the execute method, write the statement to change status of the records. 
  5. update record. 
  6. In finish method, if you need to catch the error write code for that. 
Please find sample code for Scheduler and batch Job. 
Batch Job:
global class Accountupdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,Phone FROM Account ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        List<Account> accList = (List<Account>) scope;
        for (Account a : accList)
        {
           a.Phone='123';
        }
        update a;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
Please check this link for schedulerhttps://www.minddigital.com/how-to-call-batch-apex-by-scheduler-class-within-salesforce/

Hope this helps. 

Thanks,
Gaurav
Skype: gaurav62990
Mayank.msMayank.ms
Hi Gaurav, Thanks for the reply. I know how to create scheduler class but need the solution
global class LeadLifeCycleWorkflow implements Database.Batchable<sObject>
{
     global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Stage_days__c,Stage_date__c,Status,OwnerId FROM Lead WHERE Status in (\'Contacted Attempt Made\', \'Discovery\') and Stage_days__c>20' ;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
     List<Lead> listLead = new List<Lead>();
            for(Lead l : scope)
            {
                if(l.Stage_days__c==30){
                    l.Status='Open';
                    listLead.add(l);
		    fetchTaskfor30days(l.Id)
                }
             
                
            } 
           if(!listLead.IsEmpty()){
                update listLead;
             } 
    }
     
     global void fetchTaskfor30days(String Id, Date stageDate){
           List<Task> taskData = [select createdDate,status from Task where WhoId IN :Id order by createdDate desc limit 1];
          
         	for(Task t : taskData)
            { 
             Date taskdate = t.createdDate;
             Integer noofDays = stageDate.daysBetween(taskdate) + 1;   
                if(noofDays>30){
                  
                }
             	   
            }			      
	    } 	
      
  
}
BALAJI CHBALAJI CH
Hi Kamran,
Can you please below code,
 
global class LeadLifeCycleWorkflow implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Stage_days__c,Stage_date__c,Status,OwnerId, (select id, createdDate,status from Tasks ) FROM Lead WHERE Status in (\'Contacted Attempt Made\', \'Discovery\') and Stage_days__c>29' ;
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    {
        List<Lead> listLead = new List<Lead>();
        for(Lead l : scope)
        {
            if(l.Stage_days__c==30 && l.Tasks.size()==0)
            {
                for(Task t : l.Tasks)
                {
                    if(t.createddate > l.Stage_date__c)
                    {
                        l.Status='Open';
                        listLead.add(l);
                    }
                }
            }  
        } 
        if(!listLead.IsEmpty()){
            update listLead;
        } 
    }
    
    global void finish(Database.BatchableContext BC)
    {
        
    }
}

Let us know if that works for you.

Best Regards,
BALAJI
This was selected as the best answer
GauravGargGauravGarg
Hi Kamran,

Here we are to guide you, not to provide you the exact solutions. 

Thanks,
Gaurav
Mayank.msMayank.ms
Thanks BALAJI for your reply. It will be work for me.

Thanks a lot :)