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
Prachi PotdarPrachi Potdar 

ApexTrigger

  1. 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.  Please Any Solution.TIA.
SwethaSwetha (Salesforce Developers) 
HI Prachi,
You can try the scheduled batch apex approach mentioned inhttps://salesforce.stackexchange.com/questions/156217/how-to-write-a-batch-class-for-scheduled-deletion/156307 (You will need to customize as per your requirement)

If this information helps, please mark the answer as best. Thank you
CharuDuttCharuDutt
Hii Prachii
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!

 
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Prachi,
Greetings!

In this question, you can use schedule class which will check for Account object records created in the last 24 hour period (12 am to 12 pm) every 15 minutes,
inside the scheduled class, you can write the function which check about number of accounts created in last 24 hour and if the number of records greater than 10 simply 
call a trigger function that will delete the account record above 10.

To know more about the scheduler class in salesforce you can refer to the below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

---------------------------------------------------------------------------
If this helped you, please mark it as the best answer. 

Thank you!

Regards,
Suraj Tripathi
Naveen KNNaveen KN
Out of curiosity, why dont we restrict creation of accounts if it is morethan 10 for that date instead of allowing it to create and delete it again. Am I missing something here?