0

I am just wondering, which Salesforce Admin user will be the CreatedBy/Modified for the records that are upserted in a batch class? Is there a special setting where I can see/modify/choose a specific user to set as default? Or should I assign a specific user in the batch class's code itself?

All I can see is: Process Automation Settings >> Default Workflow User(which is set to a specific user).

Will this user be defaulted under the System context while executing the batch class?

1 Answer 1

5

The running user or context user for a batch class is the user who enqueued or scheduled the batch.

If a user goes into the Developer Console and executes

System.executeBatch(myBatch, 200);

that user is the running user and changes made by the batch will have their name in the audit fields.

If a user schedules a Scheduled Apex class that kicks off a batch, or makes some record update that fires a trigger than enqueues a batch, or in some other way precipitates the asynchronous execution of the batch, that user will be the context user and reflected by the audit fields.

You can't change or set this behavior at the system level; you must ensure that the batch job is enqueued as the user in whose context you'd like it to run. In many orgs, that will be a "System" or "Integration" or similar user.

This is true across all asynchronous functionality in Salesforce, the notable exception being Platform Events, whose triggers execute in the context of the Automated Process system user.

3
  • So in my case, after I(as a Sys admin) schedule an Apex job, all the new records which are created using that batch class have my name as CreatedBy/Modified by? Commented Aug 15, 2018 at 18:12
  • 1
    That is correct.
    – David Reed
    Commented Aug 15, 2018 at 18:12
  • Perfect complete answer, I really appreciate the last part about Platform events. Commented Aug 15, 2018 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .