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
Anshuman ParhiAnshuman Parhi 

salesforce requirement

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 Account object records created in last 24 hour period (12 am to 12 pm).  If the count exceeds 10, delete the additional records. Counter will reset at 12 am to 0.
Best Answer chosen by Anshuman Parhi
CharuDuttCharuDutt
Hii Anshuman
Try Below Code
global class DailyAccountProcessor implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
         integer a = 0;
       list<Account> lstAccDelete = new list<Account>();
       list<Account> lstAcc = new list<Account>();
        for(Integer i=0;i<10;i++){
            Account Acc= new Account();
            Acc.Name = 'Test ' + i;
            /*Fill Other Necessary Fields*/
            lstAcc.Add(Acc);
        }
        insert lstAcc;
       list<Account> extraAcc = [select Id,Name,CreatedDate From Account where CreatedDate = Today order By CreatedDate DESC];
        For(Account Acc : extraAcc){
            a++;
            system.debug('How Many Records Inserted Today this Includes Testclass Records Also ' + a);
            if(a>=11){
                lstAccDelete.Add(Acc);
            }
        }
        delete lstAccDelete;
        list<Account> Acc = [select Id,Name,CreatedDate From Account where CreatedDate = Today order By CreatedDate DESC];
        system.debug('After dleteing Exrta Records' + Acc.size());
          system.debug(Acc);
    }
}


Test Class

@isTest
public class DailyAccountProcessorTest {
     public static String CRON_EXP = '0 0 0 2 6 ? 2022';
@istest
    public Static void unitTest(){
         list<Account> lstAcc = new list<Account>();
        for(Integer i=0;i<20;i++){
            Account Acc= new Account();
            Acc.Name = 'Tester ' + i;
            /*Fill Other Necessary Fields*/
            lstAcc.Add(Acc);
        }
        insert lstAcc;
            
        String jobId = System.schedule('test', CRON_EXP, new DailyAccountProcessor());
    }
}
Please Mark it As Best Answer  If It Helps
Thank You!