Getting UserInfo in Scheduled Batchable

Suppose you have an app that schedules Batchables to run. If you wanted to access say the user ID and their email inside a batchable, so specifically in the execute(), how exactly can this be done?

What our app is doing is performing callouts, BUT we need the user information when sending the HTTP request to our api from salesforce.

Any help is appreciated, thanks.

Answer

For this, you can use:

UserInfo.getUserId();
UserInfo.getUserEmail();

See the documentation here for a full list of static methods that may be useful to you in this context.

Note, this will get information for the user that scheduled the Job in the first place which may/may not be what you need.

If you needed the same details for say a record owner, you would need to access that from the User object (using the OwnerId field as a lookup to User) within the execute() method itself.

Attribution
Source : Link , Question Author : 1337holiday , Answer Author : Phil Hawthorn

Leave a Comment