2

Ok so like I explained in a previous post were having an issue getting UserInfo inside a batchable that is scheduled to run every so often. The code below outlines how were are trying to access the user's

global class PostInstall implements InstallHandler {
    global void onInstall(InstallContext context){
        insert new MyApp__MySetting__c(
            MyApp__userid__c = UserInfo.getUserId(),
            MyApp__email__c = UserInfo.getUserEmail()
        );
        System.schedule('Initialize Test',  '0 5 * * * ?', new EmailRecSchedable());
    }

}

//this class should schedule the batchable to run
global class EmailRecSchedable implements Schedulable{
    global void execute(SchedulableContext ctx){   
        BatchJob b = new BatchJob();
       database.executebatch(b);
    }
}

//this is a quick test we did to see if we can get the user's email

global class BatchContactMonkeyJob implements Database.Batchable<sObject>,Database.Stateful{

    global BatchContactMonkeyJob(){
        //send test email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddress = new String[] {'[email protected]'};
        mail.setToAddresses(toAddress);
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('CM Test');

        MyApp__MySetting__c c = MyApp__MySetting__c.getInstance(UserInfo.getUserId());

        mail.setSubject('Your Setting : ' + c.MyApp__email__c);
        mail.setPlainTextBody('Your ID: ' + UserInfo.getUserId());
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Once I install the app on another account, here is the response I get from salesforce. I should be seeing "Your Setting: " but as you can see from the screenshot, it says "[email protected]".

enter image description here

I really need this issue solved so, any suggestions or sample code is appreciated, thanks!

4
  • Have you seen this post? salesforce.stackexchange.com/questions/5964/… Commented Apr 12, 2013 at 21:36
  • I think that question is suggesting something different isnt it? Suppose you download our app, I want you to be able to send out emails and set YOUR address as "from". Commented Apr 12, 2013 at 21:58
  • In which case, if you view the source of your email you'll probably see From: CM Test <[email protected]> Sender: <[email protected]> meaning that if you hit reply it will go to [email protected] I think Commented Apr 12, 2013 at 22:06
  • 1
    yep, i set that manually. Ok we ran a test, if we schedule the job manually from the user's account, everything works, we can get their email. However when PostInstall runs, the job is scheduled as OUR app, which i believe is the issue. Commented Apr 12, 2013 at 22:14

1 Answer 1

3

I think you've figured it out, but I wanted to share what we have learned at my company as well:

  1. If you schedule jobs during PostInstall, the job is scheduled as a user that does not actually exist - a "phantom" Installation User. It will display the user's name as the name of your application.

  2. The user appears to have all privileges, but potentially only during installation?

I'm not sure what you intend on doing with your batch jobs, but I am sharing this info with you because we had scheduled jobs that were being scheduled on PostInstall, and the scheduled jobs were failing because the Installation User did not have access to update certain fields on the Account object. We checked all of our profiles, and all of our profile settings had the correct access. The issue seemed to be a result of the scheduled job being scheduled in PostInstall.

To rectify this issue, we have a Configure page for our application, and we have the user installing our application click on a button to schedule the batch jobs. Having this button has proved useful because if scheduled jobs are ever unscheduled, the user can just click this button to re-schedule them.

See this article: http://zachelrath.wordpress.com/2012/09/06/migrate-your-post-install-and-uninstall-tasks-to-apex-install-scripts/?goback=.gde_2031398_member_160017831

and comments: http://zachelrath.wordpress.com/2012/09/06/migrate-your-post-install-and-uninstall-tasks-to-apex-install-scripts/?goback=.gde_2031398_member_160017831#comment-28

1
  • I think you're right, this may be the route we have to take :( Commented Apr 13, 2013 at 17:05

You must log in to answer this question.

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