Anonymous blocks in apex

Updated: Apr 1, 2020

Anonymous block is a unnamed apex code that gets compiled and executed but does not get stored in metadata.

It is very useful if you want to test a small lines of code.

Benefits :

1. You can execute any apex code (logic).

2. You can call an apex class method from anonymous window and see the generated logs.

3. You do not have to manually commit any database changes.

4. You can schedule a Schedulable class via anonymous window.

5. Gives the fastest way to execute your code.

Different ways to execute code in Anonymous blocks:-

1. Developer Console

2. Salesforce extensions for Visual Studio Code

3. ExecuteAnonymous()SOAP API call.

Execute Anonymous block of code in Developer Console :-