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
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 );
}
}
}
}
Below is the code:
Schedule job code:
helper class for account deletion: Code for scheduling the jobs first time
Please let me know if it answers your query.