+ Start a Discussion
sumit dsumit d 

Batch to update a checkbox field on duplicate lead records

Hi All,
I am creating a batch for the requirement in which if lead has duplicates based on name than a checkbox field named ToBeDeleted__c should set to true on the duplicate of a lead records.
Can anyone guide me with this requirement?
Thanks in advance
Best Answer chosen by sumit d
Ajay K DubediAjay K Dubedi
Hi Sumit,

* Please refer through below code it is as per your requirement:
global class Update_A_Checkbox implements Database.Batchable<sObject>
{
    global Database.QueryLocator Start(Database.BatchableContext BC)
    {
        String query = 'select id,name,ToBeDeleted__c,Owner.IsActive,CreatedDate from Lead ORDER BY CreatedDate Asc';
        return Database.getQueryLocator(query);
    }
    global void Execute(Database.BatchableContext BC,List<Lead> scope)
    {
        System.debug(scope);
        List<Lead> Lead_List = New List<Lead>();
        Map<String,Lead> Lead_Map = New Map<String,Lead>();
        for(Lead l : scope)     
        {
            System.debug(l);
            if(Lead_Map.containskey(l.Name))
            {
                l.ToBeDeleted__c = True;
                Lead_List.add(l);
            }
            else
            {    
                System.debug(l);
                if(l.Name.contains('test') || l.Owner.IsActive != True || l.CreatedDate <= System.today().addDays(-365))
                {
                    l.ToBeDeleted__c = True;
                    System.debug(l.ToBeDeleted__c);
                    Lead_List.add(l);
                }
                Lead_Map.put(l.Name, l);
            }
        }
        if(Lead_List.size()>0)
        {
            update Lead_List;
        }
    }
    global void Finish(Database.BatchableContext BC)
    {
        
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sumit,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
global class Batch_UpdateCbOnDuplicate implements Database.batchable<sobject>{
    
    global Database.queryLocator start(Database.BatchableContext ctx ) {
        String str = 'SELECT Id, Name FROM Lead';
        return Database.getQueryLocator(str);
    }
    
    global void execute(Database.BatchableContext ctx, List<Lead> leadToProcess) {
        System.debug('leadToProcess -> ' + leadToProcess);
        Set<String> orderObj = new Set<String>(); 
        List<Lead> leadsToUpdate = new List<Lead>();
        for(Lead ld : leadToProcess){
            if(orderObj.contains(ld.name)){
                ld.Lead_Cb__c = true;
                leadsToUpdate.add(ld);
            }
            else {
                orderObj.add(ld.Name);
            }
        }
        UPDATE leadsToUpdate;
    }
    
    global void finish(Database.BatchableContext ctx) {
        
    }
}

/* Execute Batch Apex
* Id batchjobid = Database.executeBatch(new Batch_UpdateCbOnDuplicate(), 200);
*/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Sumit,

* This Batch Class works as per your requirement, Please refer through it:

Batch Class--->
global class Update_A_Checkbox implements Database.Batchable<sObject>
{
    global Database.QueryLocator Start(Database.BatchableContext BC)
    {
        String query = 'select id,name,ToBeDeleted__c from Lead ORDER BY CreatedDate Asc';
        return Database.getQueryLocator(query);
    }
    global void Execute(Database.BatchableContext BC,List<Lead> scope)
    {
        List<Lead> Lead_List = New List<Lead>();
        Map<String,Lead> Lead_Map = New Map<String,Lead>();
        for(Lead l : scope)     
        {
            if(Lead_Map.containskey(l.Name))
            {
                l.ToBeDeleted__c = True;
                Lead_List.add(l);
            }
            else
            {
                Lead_Map.put(l.Name, l);
            }
        }
        if(Lead_List.size()>0)
        {
            update Lead_List;
        }
    }
    global void Finish(Database.BatchableContext BC)
    {
        
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
sumit dsumit d
Hi Khan ,
Can you explain the  Set<String> orderObj = new Set<String>();
How you use this set?
sumit dsumit d
Hi Ajay,
           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 any of above condition met.
How to do it ?Can you guide me with it?
 i created a batch given below but its not working ,can you help me with what i am missing?
 
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'+
            'ORDER BY CreatedDate Asc';
        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>();
        Map<String,Lead> mapDuplicateLeadName  = New Map<String,Lead>();
        
         for(Lead l : leads ) {
             if(mapDuplicateLeadName.containskey(l.Name) )
            {
                l.ToBeDeleted__c = True;
                leadListToBeUpdated.add(l);
            }
            else
            {
                mapDuplicateLeadName.put(l.Name, l);
            }
            
            if(l.CreatedDate <= System.today().addDays(-365) 
               || l.Owner.IsActive != True
               || l.Name.contains('%Test%') 
               || l.Name.contains('%test%') 
              )
            {
                    l.ToBeDeleted__c = true;
                    leadListToBeUpdated.add(l);
                    System.debug('Checkbox'+l.ToBeDeleted__c);
                 
            } 
        }
        
         if(leadListToBeUpdated.size()>0) {
             update leadListToBeUpdated;
             System.debug('dupLeads**'+leadListToBeUpdated);
        }
     }
    
    
    public void finish(Database.BatchableContext BC){
    }
    
}
Ajay K DubediAjay K Dubedi
Hi Sumit,

* Please refer through below code it is as per your requirement:
global class Update_A_Checkbox implements Database.Batchable<sObject>
{
    global Database.QueryLocator Start(Database.BatchableContext BC)
    {
        String query = 'select id,name,ToBeDeleted__c,Owner.IsActive,CreatedDate from Lead ORDER BY CreatedDate Asc';
        return Database.getQueryLocator(query);
    }
    global void Execute(Database.BatchableContext BC,List<Lead> scope)
    {
        System.debug(scope);
        List<Lead> Lead_List = New List<Lead>();
        Map<String,Lead> Lead_Map = New Map<String,Lead>();
        for(Lead l : scope)     
        {
            System.debug(l);
            if(Lead_Map.containskey(l.Name))
            {
                l.ToBeDeleted__c = True;
                Lead_List.add(l);
            }
            else
            {    
                System.debug(l);
                if(l.Name.contains('test') || l.Owner.IsActive != True || l.CreatedDate <= System.today().addDays(-365))
                {
                    l.ToBeDeleted__c = True;
                    System.debug(l.ToBeDeleted__c);
                    Lead_List.add(l);
                }
                Lead_Map.put(l.Name, l);
            }
        }
        if(Lead_List.size()>0)
        {
            update Lead_List;
        }
    }
    global void Finish(Database.BatchableContext BC)
    {
        
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer