User Story
As a Salesforce administrator, I need to delete all secondary contacts from the Contact object. To accomplish this, I will use a batch apex class to process the records in batches.
I will define the query to identify the secondary contacts in the anonymous window using the SOQL query language. The query will select the Id of all contacts with a Level__c field equal to 'Secondary'.
Apex Class
public class UpdateAccountFields implements Database.Batchable<sObject>{
public final String Query;
public UpdateAccountFields(String q){
Query=q;
}
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC,
List<sObject> scope){
delete scope;
}
public void finish(Database.BatchableContext BC){
}
}
Apex Code
String q = 'SELECT Id FROM Contact WHERE Level__c = \'Secondary\'';
Id batchInstanceId = Database.executeBatch(new UpdateAccountFields(q), 5);
Video
Video does not exists.