Anonymous Apex Actions - sfdx-mass-action-scheduler/sfdx-mass-action-scheduler GitHub Wiki
Home > Actions > Anonymous Apex
Execute Apex code without deploying it to the org.
Code Syntax
For anonymous blocks, your script must include the following method definition, which will be passed a list of maps that represent the current batch of source records. Each execution of your script occurs in its own transaction.
void execute( List<Map<String, Object>> sourceRecordsBatch ) {
// your logic here
}
Invocable Apex and Anonymous Apex actions are invoked once per batch of source records, just like the execute method of a Batchable Class or a Bulk Trigger. However, unlike batchable or trigger code, no state is preserved within the script between executions. Though you may choose to manage and preserve state yourself between executions via DML to records or custom settings.
Code Limits
The SOAP API supports Apex scripts up to 32,000 bytes. Scripts larger than that fail with a "Script too large" exception.
To make the source data available to your script, it is inlined into your script. The JSON serialization of the source records counts towards the 32,000 byte limit.
If you receive the "Script too large" exception, you can:
- Reduce the Batch Size on the Mass Action Configuration record
- Reduce the amount of anonymous Apex code in your script
- Move parts or all of your anonymous Apex code to deployed classes in the org
Best Practices
- The context user should have the Author Apex permission.
- To mitigate the "Script too large" limits, avoid long text area fields in your data sources.
Resources
- Apex Basics & Database (Trailhead)
- Build Apex Coding Skills (Trailhead)
- Apex Developer Guide (documentation)
- Anonymous Blocks (documentation)