Anonymous blocks in apex

Updated: Apr 1

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 :-

This is the most common way to use anonymous window. To open it go to your salesforce org and follow the steps.

1.Click Setup and select Developer Console

2.In the Developer Console, click Debug | Open Execute Anonymous Window.

3.If there is already code in the Enter Apex Code window, replace it with this code:

4.Select Open Log and click on execute. Execution log will open.

5.Select Debug Only. The Details column displays the contents of the debug statements in your code. It will display account id and name as shown in below image.

Execute Anonymous block of code in SOAP API :-

ExecuteAnonymous call is available through Apex API&Tooling API.

1.Apex API is available through SOAP protocol.

2.Tooling API is available through SOAP as well as REST.

Syntax :

Limitations :

You cannot use keyword 'Static' in anonymous code.


©2019 by SFDC Blogs.