I have created a package in salesforce that will make an http callout request with user's email id to get data for that email and add the data to some custom object. But when I tried using UserInfo.getUserEmail() as a request parameter but it makes a request with noreply@salesforce.com. It does work when I run it on the dev account. I also tried using

1.String userName = UserInfo.getUserName();
2.User activeUser = [Select Email From User where Username = : userName limit 1];
3.String userEmail = activeUser.Email;

But line no 2 gave me an error saying List has no rows for assignment to SObject

This is my first salesforce package, so any help would be appreciated.

  • 1
    Can you clarify in what context your code is being run? Is this in a trigger, a vf page? What user is logged in? – Ralph Callaway Apr 7 '13 at 4:02
  • The code is running in batch class which is scheduled to run and pull data every hour and the user is whoever installed our package. By logged in, i meant the active user for that package. Let me know if I wasn't clear on anything. – sPaz Apr 7 '13 at 4:19
  • Very strange this sounds like a bug, based on my experience UserInfo.getUserEmail() should return the email for the user who scheduled the batch class. Nothing you're doing sounds out of the ordinary. I'd recommend creating a case with Salesforce support – Ralph Callaway Apr 7 '13 at 5:06
  • 1
    Thanks for the advice. Apparently when I try to create case from <help.salesforce.com/HTUnauthContactSupport>, it redirects me to <help.salesforce.com/hthome?err=1> page. UGH! I am starting to hate salesforce. This could have be because I don't have support plan, but I dont even see any feedback or options. – sPaz Apr 7 '13 at 5:40
  • OMG, that is brutal. AFAIK everyone gets support, the free support is just much slower. – Ralph Callaway Apr 7 '13 at 6:04

How do you setup the schedule to call the batch class?

If the schedule is created as part of the package installation (via a class that implements InstallHandler) then it will likely be running as the special installer 'God' or 'phantom' user. This user doesn't exist (or at least is not accessible) in either the packaging or destination org. If this is the case use the installerId to get the user ID of the user who initiated the installation.

The schedule that starts the batch also runs as System (See the Apex Scheduler docs) and calls to UserInfo will only show the details of the special InstallHandler user.

In both cases the User that is running the batch will not be the user that installed the package.

Maybe you can use something like a custom setting to store the installerId and then query that in the batch job. Or pass the installerId into the Batch classes constructor and use it to modify the SOQL query that is passed to Database.getQueryLocator() (assuming the initial query is from User).

| improve this answer | |
  • My scheduler calls schedulable class, which has execute method that does:CalloutBatch b = new CalloutBatch (); database.executebatch(b,1); Schedule is created via installhandler interface indeed. I had a doubt about what you said, how it runs as a 'phantom' user. Thanks for clearing that up for me. Can you tell me how I can go about it? (How I can save installer id in settings or something to schedule it for them) Or if I can save all their Id's in my account by initializing new table and I can run batch for all users that installed the app from my account? (Not sure if it's possible) – sPaz Apr 7 '13 at 18:44
  • 1
    Is that really the case that a batch initiated by a scheduled class doesn't run as the user who initiated it? I understand the install handler since that's one of the virtual users, but the doc reference in your answer is the same for triggers (they run as system), but you still have a user context. – Ralph Callaway Apr 7 '13 at 19:24
  • I can't think of any other reason of why this was happening. And if you are talking about context on install, it does have installcontext that has user id. I would need a way to save it or pass it in parameter for schedulable class I believe to make it work. Even if I do save it, I am not sure how i will make 'phantom' process execute for that id. – sPaz Apr 7 '13 at 19:43
  • 2
    @Ralph. Hmm, yes. I might have been a bit misleading with the batch running as System comment. I think the important part is that the schedule is being created by the special InstallHandler user rather than the user that initiated the install. – Daniel Ballinger Apr 7 '13 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.