You need to sign in to do that
Don't have an account?
Sindhuja Sundarasudhan
deleteaccount Apex code
Can anyone tell me what is my error in this code to delete accounts details in salesforce. I am getting DMLexception error when I execute.
global class deleteaccount implements Database.Batchable<sobject> {
global final string Query;
global Database.QueryLocator start(Database.BatchableContext BC)
{
string query = 'select Id, name from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, list<Account> scope)
{
List<Account> lstAccount = new list<Account>();
for(Sobject s : scope){
Account a = (Account)s;
lstAccount.add(a);
}
Delete lstAccount;
}
global void finish(Database.BatchableContext BC)
{
Messaging.singleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'sudhansindhuja@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('job done');
mail.setPlainTextBody('batch class over');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
global class deleteaccount implements Database.Batchable<sobject> {
global final string Query;
global Database.QueryLocator start(Database.BatchableContext BC)
{
string query = 'select Id, name from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, list<Account> scope)
{
List<Account> lstAccount = new list<Account>();
for(Sobject s : scope){
Account a = (Account)s;
lstAccount.add(a);
}
Delete lstAccount;
}
global void finish(Database.BatchableContext BC)
{
Messaging.singleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'sudhansindhuja@gmail.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('job done');
mail.setPlainTextBody('batch class over');
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
}
}
--Identify the Entitlements specified on the error.
--Open the Entitlements tab and search for the 'Entitlements' in the error.
--Either delete the Entitlement/s or associate it to a dummy Account.
Also one sugggestion is to use Database.delete() method instead of delete() as you can partially delete records in case any batch fails while running due to an error. check here- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_deleteresult.htm
All Answers
--Identify the Entitlements specified on the error.
--Open the Entitlements tab and search for the 'Entitlements' in the error.
--Either delete the Entitlement/s or associate it to a dummy Account.
Also one sugggestion is to use Database.delete() method instead of delete() as you can partially delete records in case any batch fails while running due to an error. check here- https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database_deleteresult.htm