Executing Anonymous Apex code is useful for running ad-hoc operations or administrative tasks in Salesforce.
Option A: To run a batch Apex class to update all Contacts
Database.executeBatch(new UpdateContactsBatchClass());
[Reference:, Batch Apex, Anonymous Blocks, Option D: To schedule an Apex class to run periodically, Valid Use Case., Explanation:, Anonymous Apex can be used to schedule an Apex class using System.schedule., This is useful for setting up scheduled jobs without deploying code., Example:, , String sch = '0 0 12 * * ?'; // Cron expression for daily at noon, System.schedule('Daily Job', sch, new ScheduledApexClass());, Reference:, Scheduling Apex, Anonymous Apex Execution, Options Not Suitable:, Option B: To delete 15,000 inactive Accounts in a single transaction after a deployment, Not Feasible Due to Limits., Explanation:, The DML operation limit per transaction is 10,000 records., Deleting 15,000 records in a single transaction would exceed this limit and result in a governor limit exception., This operation would need to be done in batches., Option C: To add unit test code coverage to an org, Not Applicable., Explanation:, Anonymous Apex code does not contribute to code coverage., Code coverage is achieved by writing test classes and methods annotated with @isTest., Conclusion:, The two valid use cases for executing Anonymous Apex code are:, Option A: Running a batch Apex class to update all Contacts., Option D: Scheduling an Apex class to run periodically., ]
Submit