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
Sindhuja SundarasudhanSindhuja 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});
    }
}
Best Answer chosen by Sindhuja Sundarasudhan
Ashish  VermaAshish Verma
This is because there is an account which is associated with an entitlement record in your org.
--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

Ashish  VermaAshish Verma
Hi , can you paste the exact exception here?
Sindhuja SundarasudhanSindhuja Sundarasudhan
EXCEPTION_THROWN [17]|System.DmlException: Delete failed. First exception on row 3 with id 0018c000027CXOJAA4; first error: DELETE_FAILED, Your attempt to delete Sample Account for Entitlements could not be completed because it is associated with the following entitlements.: Sample Entitlement
Ashish  VermaAshish Verma
This is because there is an account which is associated with an entitlement record in your org.
--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
This was selected as the best answer
Sindhuja SundarasudhanSindhuja Sundarasudhan
Thank you So much.