function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh ERakesh E 

who is the current user in batch job execution.

 

Hi all,

 

i have a question. we have a scenario of sending email to the current user from batch job. but batch jobs are asynchronous and will run in the backend even if we are not logged into the salesforce org. so when that batch code gets the mail id of current user using

 

userinfo.getuserId() method , which user will exactly get the mail. please reply if anyone has idea about this.

 

 

Thanks and Regards

Rakesh

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Your question is unclear. Like I said, the user that starts a transaction will own that transaction, and a scheduled job will be executed on behalf of the user that scheduled the job. This is the same concept as every other multi-user system where users can schedule jobs: UNIX's "cron", Windows "Task Scheduler", and so on. Even if the user is not logged in, their actions will be performed, as them, regardless.

 

Users of salesforce.com can all be logged in to the same database at the same time. The system will not confuse the scheduled job owner even if that user logs out and other logs in; in fact, a single user can be logged in multiple times, and many users can (and usually are) logged in at once. If the user changes their email address, then the emails will go to the new address when the next job runs (if that job sends emails).

 

UserInfo.getUserId() has four use cases: 1) normal usage, e.g. any transaction run in the system that is not an install or "test method." 2) test methods, logic that is written to verify that code is working correctly (when using System.runAs), 3) during installs/uninstalls, which uses a special user that represents the package itself, and 4) a guest user, portal user, or other special user when various portals are used. There is no difference in usage in regards to "general execution" as opposed to batch and scheduled jobs; they will all return the same user ID, which will always be the user.

All Answers

sfdcfoxsfdcfox

The user that triggers the code will be the executor of the batch. If scheduled, it will be the user that scheduled the class.

Rakesh ERakesh E

Thank you for reply.

 

i am confused. what if one user scheduled a job which uses the userinfo.getuserId() method say for sending a mail.

 

by the time the sceduled job gets executed if another user has logged into the org. then which user will get the mail?

 

and one more question, is the way userinfo.getuserId() gets executed differs from general execution to Batch/Scheduled job executions?

 

please make it clear.

 

Regards

Rakesh

sfdcfoxsfdcfox

Your question is unclear. Like I said, the user that starts a transaction will own that transaction, and a scheduled job will be executed on behalf of the user that scheduled the job. This is the same concept as every other multi-user system where users can schedule jobs: UNIX's "cron", Windows "Task Scheduler", and so on. Even if the user is not logged in, their actions will be performed, as them, regardless.

 

Users of salesforce.com can all be logged in to the same database at the same time. The system will not confuse the scheduled job owner even if that user logs out and other logs in; in fact, a single user can be logged in multiple times, and many users can (and usually are) logged in at once. If the user changes their email address, then the emails will go to the new address when the next job runs (if that job sends emails).

 

UserInfo.getUserId() has four use cases: 1) normal usage, e.g. any transaction run in the system that is not an install or "test method." 2) test methods, logic that is written to verify that code is working correctly (when using System.runAs), 3) during installs/uninstalls, which uses a special user that represents the package itself, and 4) a guest user, portal user, or other special user when various portals are used. There is no difference in usage in regards to "general execution" as opposed to batch and scheduled jobs; they will all return the same user ID, which will always be the user.

This was selected as the best answer
Rakesh ERakesh E

 

Thank you and i am sorry for asking the same thing again and again.

 

i understood that the transaction holder will beome the current user when the scheduled job gets executed.

 

what if we want only current logged in user has to get mail , regardless of which user has sceduled the job/ started the batch job. is it possible to do that.

 

Regards

Rakesh 

sfdcfoxsfdcfox

The system is a multi-user system. Your question has no meaning, because as far as the server is concerned, no users are logged in at all. Each transaction is handled independently, authenticated independently using session ID values, etc. You'll have to find a different way to approach your problem.

Rakesh ERakesh E

Thank you so much for giving a detailed explanation. its clear now

 


Regards

Rakesh