8

This is my scenario: I have a managed package, with quite some logic in it, and 2 triggers on normal SObjects, Lead and Contact the 2 triggers populate a field (included in the managed package) on the corresponding SObjects in the beforeUpdate and beforeInsert. (those fields are necessary for my package to properly work)

I wrote 2 simple batch jobs (see below) with the goal of populating those fields when the package gets installed on an org (in case the org has Leads and Contacts already, and those fields weren't populated). And an install Script (see below), which just starts both of the batch jobs, I then added the Install Script in the "Install Script" field on the managed package.

When the package installs, the code gets executed properly, the batches start, but when I look at the Apex Jobs the 2 batches failed with the error messages:

Batch Apex Failed First error: sObject type 'Lead' is not supported.

Batch Apex Failed First error: sObject type 'Contact' is not supported.

Here is the Batch (as you can see the only thing this batch does, is making sure the contacts go through my trigger)

public with sharing class ContactPopulatePackageFieldBatch implements Database.Batchable<sObject>
{
    public Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id, PackageField__c FROM Contact WHERE PackageField__c = NULL OR PackageField__c = \'\'';
        return Database.getQueryLocator(query);  
    }

    public void execute(Database.BatchableContext BC, List<Contact> scope)
    {
        Database.update(scope,false);
    }

    public void finish(Database.BatchableContext BC)
    {  
        // Nothing to see here
    }
} 

Then I created an InstallScript to trigger those batch jobs on install of my package:

public with sharing class InstallLogic implements InstallHandler
{
    public void onInstall(InstallContext context)
    {
        // start a lead batch
        LeadPopulatePackageFieldBatch leadBatch = new LeadPopulatePackageFieldBatch();
        Database.executeBatch(leadBatch,1000);

        // start a contact batch
        ContactPopulatePackageFieldBatch contactBatch = new ContactPopulatePackageFieldBatch();
        Database.executeBatch(contactBatch,1000);
    }
}
  • What type of org are you installing into? – Matt Lacey Mar 7 '13 at 11:33
  • Enterprise Org (from a Partner Development Org) – pjcarly Mar 7 '13 at 11:39
  • Do the batches work when you call them outside of the InstallHandler Interface? – ReidCarlberg Mar 7 '13 at 11:44
  • I have no idea how, the 2 batches are inside a managed package, with visibility public (not global), and I would like to keep it that way, don't want clients to go executing this code. – pjcarly Mar 7 '13 at 12:06
  • What happens if you cast the scope to the concrete object e.g. Contact, and then use Database.Update? – BritishBoyinDC Mar 7 '13 at 17:54
7

In order for your Batch Apex to work from the InstallScript context, both your Install Script and the Batch Apex class will have to be marked without sharing, or at least you will need to remove the with sharing, e.g.

public class InstallLogic implements InstallHandler

or

public without sharing class InstallLogic implements InstallHandler

and, for your Batch Apex class,

public class ContactPopulatePackageFieldBatch implements Database.Batchable<sObject>

or

public without sharing class ContactPopulatePackageFieldBatch implements Database.Batchable<sObject>

See this article for an explanation of some of the quirks of Install Scripts that are causing your "SObject type is not supported" exceptions.

0

I notice you have a custom field in your query. That does in fact need a namespace, when referenced from your install script. Have you tried adding the package namespace to that or using non-dynamic soql?

The only other thing I can think of is that managed packages dont support profile or permission settings for standard objects, wondering if that is somehow mucking with your iterator.

  • I will try that out tomorrow, and keep you posted, thanks for posting the suggestion. – pjcarly Mar 7 '13 at 21:00
  • Just tried it out, no luck here, I will open a Case on the ISV Partner Portal and keep you posted – pjcarly Mar 8 '13 at 13:28
  • @zachelrath his answer is correct, closed my case on the partner portal – pjcarly Mar 9 '13 at 8:53

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.