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
Pradeep Pradhan 21Pradeep Pradhan 21 

2.Client wants only upto 10 records to be created on the Account object daily. Write code which will run every 15 minutes. It will check for count of Account object records created in last 24 hour period . If the count exceeds 10, delete t

Shubam GuptaShubam Gupta
Hi Pradeep,

Below is the code:

Schedule job code:
global without sharing class ScheduleAccountDeletion implements Schedulable {
    public void execute (SchedulableContext sc){
       AccountCount.ExtraAccountDeletion(); 
    }

}

helper class for account deletion:
public class AccountCount {
    public static void ExtraAccountDeletion(){
        List<Account> accList = [Select Id from Account where createdDate >=Today order by CreatedDate asc];
        List<Account> accListDeletion = new list <Account>();
        if(accList.size()>10){
            for(integer i=10; i<accList.size();i++){
                accListDeletion.add(accList[i]);
            }
        }
        
        if(accListDeletion.size()>0){
            try{
                Database.Delete(accListDeletion);
            }catch(exception e){
                System.debug('Exception: '+e );
            }
        }
    }

}
Code for scheduling the jobs first time
ScheduleAccountDeletion accDeletion = new ScheduleAccountDeletion();
System.schedule('Extra Account Deletion: at 00 mins', '0 0 * * * ?', accDeletion);
System.schedule('Extra Account Deletion: at 15 mins', '0 15 * * * ?', accDeletion);
System.schedule('Extra Account Deletion: at 30 mins', '0 30 * * * ?', accDeletion);
System.schedule('Extra Account Deletion: at 45 mins', '0 45 * * * ?', accDeletion);

Please let me know if it answers your query.