Apex – DML Operation Delete Not Allowed on List

apexbatchdml

I have a scheduled job named "Scheduled_Mail" and its class is "Scheduled_Mail_Controller".
From February 13, suddenly the following error occurs.

'Scheduled_Mail' : Dependent class is invalid and needs recompilation: Class Schedule_Notice_Batch_Controller : DML operation Delete not allowed on List<Notification__c>

Scheduled_Mail_Controller

global without sharing class Scheduled_Mail_Controller implements Schedulable {
    global void execute(SchedulableContext ctx) {
        Schedule_Notice_Batch_Controller noticeBatch = new Schedule_Notice_Batch_Controller();
        Database.executeBatch(noticeBatch, 200);

Schedule_Notice_Batch_Controller

    public without sharing class Schedule_Notice_Batch_Controller implements Database.Batchable<sObject>{
    
    public Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id,CreatedDate FROM Notification__c ORDER BY CreatedDate DESC';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext bc, List<sObject> targetRecords) {
        List<Notification__c> deleteNoticeList = new List<Notification__c>();
        for(sObject s: targetRecords){
            if((Datetime)s.get('CreatedDate')<=System.today().addDays(-90)){
                deleteNoticeList.add((Notification__c)s);
            }
        }
        
        if(deleteNoticeList.size()>0){
            if (Notification__c.sObjectType.getDescribe().isDeletable()){
                delete deleteNoticeList;
            }
        }
    }
    
    public void finish(Database.BatchableContext bc) {
    }
}

If you know how to solve this problem, please kindly tell me.
Thank you.

Best Answer

This started failing in Spring '22. We have recently encountered this problem, too. This happens if you schedule a job from PostInstallHandler (my related question: Debug Logs for PostInstallHandler SuperUser).

For me, it helped to build new version of app in which I aborted previously scheduled jobs (that were scheduled from PostInstallHandler by a SuperUser mentioned in this question) and added possibility to run this scheduled job from real System Administrator (in my case - it is an Apex method called from VF page).

So the actual solution is to reschedule your jobs under actual System Administrator that has permissions on your managed package custom object on delete.

Related Topic