Can we use a single class as a batchable class and install handler?

Background

We would like to use a single class for installing our app. To avoid having any more classes than we need, we’re trying to consolidate installation into a single class.

Class

public without sharing class InstallationSystem implements Database.Batchable<SObject>, InstallHandler {
    public void onInstall(InstallContext ic) {
        // Initialize some data here
        Database.executeBatch(this);
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([some query]);
    }
    public void execute(Database.BatchableContext bc, Sobject[] records) {
    }
    public void finish(Database.BatchableContext bc) {
    }
}

Question

This code compiles, but will it successfully execute? I’m asking here rather than just dumping it in to our project because I’d like to know if this approach will work, or if I would be better off simply using two classes.

Answer

This code compiles, but will it successfully execute?

Yes. BUT:

  1. your Post Install Script runs under a special ghost User that ceases to exist after install is complete.

  2. any Batch Apex kicked off by the Post Install Script will operate under this ghost User,

  3. any records touched by your script or batch apex will be owned or modified by the ghost User,

(The ghost User does have a prefix of 005, and takes its name from the Managed Package. But it doesn’t appear in the user list and shows ‘Insufficient Privileges’ as opposed to the usual ‘Data Not Available’ if you try to view it. Very elusive!)

ghost user

Attribution
Source : Link , Question Author : sfdcfox , Answer Author : max

Leave a Comment