Anonymous Blocks

User Permissions Needed
To execute anonymous Apex:

(Anonymous Apex execution through the API allows restricted access without the “Author Apex” permission.)

“Author Apex
An anonymous block is Apex code that does not get stored in the metadata, but that can be compiled and executed using one of the following:
  • Developer Console
  • Force.com IDE
  • The executeAnonymous() SOAP API call:
    ExecuteAnonymousResult executeAnonymous(String code)

You can use anonymous blocks to quickly evaluate Apex on the fly, such as in the Developer Console or the Force.com IDE, or to write code that changes dynamically at runtime. For example, you might write a client Web application that takes input from a user, such as a name and address, and then uses an anonymous block of Apex to insert a contact with that name and address into the database.

Note the following about the content of an anonymous block (for executeAnonymous(), the code String):
  • Can include user-defined methods and exceptions.
  • User-defined methods cannot include the keyword static.
  • You do not have to manually commit any database changes.
  • If your Apex trigger completes successfully, any database changes are automatically committed. If your Apex trigger does not complete successfully, any changes made to the database are rolled back.
  • Unlike classes and triggers, anonymous blocks execute as the current user and can fail to compile if the code violates the user's object- and field-level permissions.
  • Do not have a scope other than local. For example, though it is legal to use the global access modifier, it has no meaning. The scope of the method is limited to the anonymous block.
  • When you define a class or interface (a custom type) in an anonymous block, the class or interface is considered virtual by default when the anonymous block executes. This is true even if your custom type wasn’t defined with the virtual modifier. Save your class or interface in Salesforce to avoid this from happening. Note that classes and interfaces defined in an anonymous block aren’t saved in your organization.

Even though a user-defined method can refer to itself or later methods without the need for forward declarations, variables cannot be referenced before their actual declaration. In the following example, the Integer int must be declared while myProcedure1 does not:

Integer int1 = 0;

void myProcedure1() {
    myProcedure2();
}

void myProcedure2() {
    int1++;
}

myProcedure1();
The return result for anonymous blocks includes:
  • Status information for the compile and execute phases of the call, including any errors that occur
  • The debug log content, including the output of any calls to the System.debug method (see Debug Log)
  • The Apex stack trace of any uncaught code execution exceptions, including the class, method, and line number for each call stack element

For more information on executeAnonymous(), see SOAP API and SOAP Headers for Apex. See also Working with Logs in the Developer Console and the Force.com IDE.

Executing Anonymous Apex through the API and the “Author Apex” Permission

To run any Apex code with the executeAnonymous() API call, including Apex methods saved in the organization, users must have the “Author Apex” permission. For users who don’t have the “Author Apex” permission, the API allows restricted execution of anonymous Apex. This exception applies only when users execute anonymous Apex through the API, or through a tool that uses the API, but not in the Developer Console. Such users are allowed to run the following in an anonymous block.

  • Code that they write in the anonymous block
  • Web service methods (methods declared with the webservice keyword) that are saved in the organization
  • Any built-in Apex methods that are part of the Apex language

Running any other Apex code isn’t allowed when the user doesn’t have the “Author Apex” permission. For example, calling methods of custom Apex classes that are saved in the organization isn’t allowed nor is using custom classes as arguments to built-in methods.

When users without the “Author Apex” permission run DML statements in an anonymous block, triggers can get fired as a result.

Previous
Next