You need to sign in to do that
Don't have an account?

Test class for batch apex on lead removal
Can anyone provide Test class for following scenario.Consider all positive negative Bulk testing for the given batch class.
Use Case ::
Removal of duplicate Leads
1. During the Campaigning, It might happen that representative creates duplicate leads in an org.
2. So admin want to build a process which will run every 3 hours/day & remove the duplicate leads from the org.
3. The criteria to find the duplicate records should be configurable. Ex. If there are two leads in a system with same Email address then keep the first lead entry & remove all the other leads. So the field like Email, Name which will be deciding the uniqueness should be configurable.
--------------------------------------------------BATCH class----------------------------------------------
global class RemoveDuplicateLeads implements Database.Batchable<sObject>, Database.Stateful {
/**
Map will Keep count of Lead which are having same field
*/
Map<String,Integer> countMap = new Map<String,Integer>();
/**
Constructor to initialize the countMap. Which will be useful for every batch operation.
*/
global RemoveDuplicateLeads() {
Database.QueryLocatorIterator iterator =
Database.getQueryLocator('select ' + System.Label.Lead_Field + ' from Lead').iterator();
while (iterator.hasNext())
{
Lead leadRecord = (Lead) iterator.next();
if(leadRecord.get(System.Label.Lead_Field) != null) {
countMap.put((String)leadRecord.get(System.Label.Lead_Field), 0);
}
}
}
/**
This method will start the batch execution.
@return Database.QueryLocator - This will contain all Leads.
*/
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('select ' + System.Label.Lead_Field + ' from lead');
}
/**
This method will execute for every batch and deletes the duplicate leads.
@return Nothing.
*/
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Lead> leadList = new List<Lead>();
for(SObject lead : scope) {
if(lead.get(System.Label.Lead_Field) != null) {
if(countMap.get((String)lead.get(System.Label.Lead_Field)) >= 1 ) {
leadList.add((Lead)lead);
} else {
countMap.put((String)lead.get(System.Label.Lead_Field),1);
}
}
}
Database.delete(leadList,false);
}
/**
This method will execute at end of Batch apex process.
@return Nothing.
*/
global void finish(Database.BatchableContext BC) {
}
}
Use Case ::
Removal of duplicate Leads
1. During the Campaigning, It might happen that representative creates duplicate leads in an org.
2. So admin want to build a process which will run every 3 hours/day & remove the duplicate leads from the org.
3. The criteria to find the duplicate records should be configurable. Ex. If there are two leads in a system with same Email address then keep the first lead entry & remove all the other leads. So the field like Email, Name which will be deciding the uniqueness should be configurable.
--------------------------------------------------BATCH class----------------------------------------------
global class RemoveDuplicateLeads implements Database.Batchable<sObject>, Database.Stateful {
/**
Map will Keep count of Lead which are having same field
*/
Map<String,Integer> countMap = new Map<String,Integer>();
/**
Constructor to initialize the countMap. Which will be useful for every batch operation.
*/
global RemoveDuplicateLeads() {
Database.QueryLocatorIterator iterator =
Database.getQueryLocator('select ' + System.Label.Lead_Field + ' from Lead').iterator();
while (iterator.hasNext())
{
Lead leadRecord = (Lead) iterator.next();
if(leadRecord.get(System.Label.Lead_Field) != null) {
countMap.put((String)leadRecord.get(System.Label.Lead_Field), 0);
}
}
}
/**
This method will start the batch execution.
@return Database.QueryLocator - This will contain all Leads.
*/
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('select ' + System.Label.Lead_Field + ' from lead');
}
/**
This method will execute for every batch and deletes the duplicate leads.
@return Nothing.
*/
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Lead> leadList = new List<Lead>();
for(SObject lead : scope) {
if(lead.get(System.Label.Lead_Field) != null) {
if(countMap.get((String)lead.get(System.Label.Lead_Field)) >= 1 ) {
leadList.add((Lead)lead);
} else {
countMap.put((String)lead.get(System.Label.Lead_Field),1);
}
}
}
Database.delete(leadList,false);
}
/**
This method will execute at end of Batch apex process.
@return Nothing.
*/
global void finish(Database.BatchableContext BC) {
}
}
Use this code

Hi there, the Lead_field turns out to be an External String that doesn't exist. Can you please explain how you did the question because I can totally understand the approach and the logic created but I do not know how to use the label thing. thanks in advance! :)