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
sumit dsumit d 

Batch to set checkbox field to true on lead object

Hi All,
         i have a requirement in which i have a field on lead object called ToBeDeleted__c, which should be true when one of the following condition is true :-
1.lead name contains 'test'   
2.The owner of the lead is inactive.
3.lead has duplicate records than last created record need to be checked.
4.lead is created 1 year or more than one year ago.
I want to create a batch which set the checkbox field true when above condition met.
How to do it ?Can someone guide me with it?
Thanks
Best Answer chosen by sumit d
Soyab HussainSoyab Hussain
Hi Sumit,

As per my perception, this code will work for you.
public class BatchLeadCleansing implements Database.Batchable<sObject>{
    
    //Run method to check the Batch on one record
    public static void run( Set<Id> leadIds ) {
        List<Lead> leadRecords =  [SELECT id,ToBeDeleted__c,CreatedDate,Name,Owner.IsActive 
                                   FROM Lead 
                                   WHERE ID IN : leadIds
                                  ];
        executeHelper( leadRecords );                  
    }
    
    public Database.querylocator start(Database.BatchableContext BC){
        String query ='SELECT id,ToBeDeleted__c,CreatedDate,'+
            'Name,Owner.IsActive' +
            'FROM Lead';
        return Database.getQueryLocator(query);   
    }
    
    
    public void execute(Database.BatchableContext BC, List<lead> leads ){
        executeHelper( leads );
    }
    
    public static void executeHelper( List<Lead> leads ) {
        List<Lead> leadListToBeUpdated = new List<Lead>();
        Set<String> leadNamesForUniquelyIdendify = new Set<String>();
        for(Lead l : leads ) {
            
            if(l.CreatedDate >= System.today().addDays(-365) 
               || l.Owner.IsActive != True
               || l.Name.contains('Test') 
               || l.Name.contains('test') 
               || leadNamesForUniquelyIdendify.contains(l.Name)
              )
            {
                l.ToBeDeleted__c = true;
            } 
            leadNamesForUniquelyIdendify.add(l.Name);
        }
    }
    
    
    public void finish(Database.BatchableContext BC){
    }
}
If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn: https://www.linkedin.com/in/soyab-hussain-b380b1194/  (https://www.linkedin.com/in/soyab-hussain-b380b1194/ )
Regards,
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi Sumit,

I did not understand the third point, Can you please give more description of the same.

Regards,
Soyab
sumit dsumit d
Hi Soyab,
 Third point is if two lead records has same name than the record which has created later will have the checkbox field set to true.
Hope you can guide me with it?
Thanks
David Roberts 4David Roberts 4
I would use the developer console and create and execute immediate script.
Form your query and the update on the result.
Something like this will do 1 and 4.
You need a sub query to get inactive owner.
Duplicate records is much more complex...
Hope this helps a little.
 
lead[] queriedLeads = 
        [

SELECT Name,id, description__c , description , owner.name, createddate from lead where Name like '%test%' or createddate < 2018-11-14   and isconverted = false order by createddate
        ];
    
    integer num;
    
    for (lead e : queriedLeads) {
        e.ToBeDeleted__c =  true;
        System.debug(e.name);
    }

//start with the next line commented out until you are sure you have the query correct
    //update queriedLeads;
David Roberts 4David Roberts 4
Ha! :)
It's a good idea to check your query in the Query Editor as I just did.
The correct syntax is
SELECT Name,id, description , owner.name, createddate from lead where (Name like '%test%' or createddate < 2018-11-14T23:00:00Z)   and isconverted = false order by createddate

 
David Roberts 4David Roberts 4
and the inactive user is easier than I first thought...
SELECT Name,id, description , owner.name, owner.isactive, createddate from lead where (Name like '%test%' or createddate < 2018-11-14T23:00:00Z or owner.isactive = false)   and isconverted = false order by createddate

 
sumit dsumit d
Hi David,
My batch is given below:-
 
public class BatchLeadCleansing implements Database.Batchable<sObject>{
    
    //Run method to check the Batch on one record
    public static void run( Set<Id> leadIds ) {
        List<Lead> leadRecords =  [SELECT id,ToBeDeleted__c,CreatedDate,Name,Owner.IsActive 
                                   FROM Lead 
                                   WHERE ID IN : leadIds
                                  ];
        executeHelper( leadRecords );                  
    }
    
    public Database.querylocator start(Database.BatchableContext BC){
        String query ='SELECT id,ToBeDeleted__c,CreatedDate,'+
            'Name,Owner.IsActive' +
            'FROM Lead';
        return Database.getQueryLocator(query);   
    }
    
    
    public void execute(Database.BatchableContext BC, List<lead> leads ){
        executeHelper( leads );
    }
    
    public static void executeHelper( List<Lead> leads ) {
         List<Lead> leadListToBeUpdated = new List<Lead>();
         for(Lead l : leads ) {
            if(l.CreatedDate >= System.today().addDays(-365) 
               || l.Owner.IsActive != True
               || l.Name.contains('Test') 
               || l.Name.contains('test') 
              )
            {
                l.ToBeDeleted__c = true;
            } 
        }
     }
    
    
    public void finish(Database.BatchableContext BC){
    }
     }
Can you guide me to move ahead and also please tell me about duplicate records?
Soyab HussainSoyab Hussain
Hi Sumit,

As per my perception, this code will work for you.
public class BatchLeadCleansing implements Database.Batchable<sObject>{
    
    //Run method to check the Batch on one record
    public static void run( Set<Id> leadIds ) {
        List<Lead> leadRecords =  [SELECT id,ToBeDeleted__c,CreatedDate,Name,Owner.IsActive 
                                   FROM Lead 
                                   WHERE ID IN : leadIds
                                  ];
        executeHelper( leadRecords );                  
    }
    
    public Database.querylocator start(Database.BatchableContext BC){
        String query ='SELECT id,ToBeDeleted__c,CreatedDate,'+
            'Name,Owner.IsActive' +
            'FROM Lead';
        return Database.getQueryLocator(query);   
    }
    
    
    public void execute(Database.BatchableContext BC, List<lead> leads ){
        executeHelper( leads );
    }
    
    public static void executeHelper( List<Lead> leads ) {
        List<Lead> leadListToBeUpdated = new List<Lead>();
        Set<String> leadNamesForUniquelyIdendify = new Set<String>();
        for(Lead l : leads ) {
            
            if(l.CreatedDate >= System.today().addDays(-365) 
               || l.Owner.IsActive != True
               || l.Name.contains('Test') 
               || l.Name.contains('test') 
               || leadNamesForUniquelyIdendify.contains(l.Name)
              )
            {
                l.ToBeDeleted__c = true;
            } 
            leadNamesForUniquelyIdendify.add(l.Name);
        }
    }
    
    
    public void finish(Database.BatchableContext BC){
    }
}
If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn: https://www.linkedin.com/in/soyab-hussain-b380b1194/  (https://www.linkedin.com/in/soyab-hussain-b380b1194/ )
Regards,
Soyab
 
This was selected as the best answer