Salesforce Apex: Execute Anonymous Batch
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ExecuteAnonymousBatch.cls
ExecuteAnonymousBatchTest.cls
README.md

README.md

Execute Anonymous Batch

Author: Enrico Murru

Read details @ http://blog.enree.co/2017/08/salesforce-apex-execute-anonymous-batch.html

The ID_LIST global variable is of type List<ID> and contains all the IDs provided by the Batch's scope.

Remember to configure a named credential called EXECUTE_ANONYMOUS that points to your instance (and that is logged with a valid session ID).

Example of usage:

String script = 'List<Account> acList = [Select Id, Name From Account Where Id IN :ID_LIST];' 
				+'\nfor(Account acc : acList){'
				+'\n   acc.BillingCity = \'Gnite City\';'
				+'\n}'
				+'\n update acList;';

String query = 'Select Id From Account Where BillingCity != \'Gnite City\'';

Boolean sendEmailOnFinish = true;

ExecuteAnonymousBatch batch = new ExecuteAnonymousBatch(query, script, sendEmailOnFinish);
Database.executeBatch(batch, 100);

The job sends a completion email with all eventual errors on finish.