[SalesForce] Managed package can’t access class defined in subscriber org via Type.forName

I'm in the process of making a managed package extensible via an Apex plugins pattern. Basically there is an interface defined in the managed package that the subscriber Orgs can implement and then provide the class name to the managed package via a custom setting. The managed package creates an instance of the subscriber orgs class, casts it to the interface and then invokes the required interface methods.

I'm following the examples from New Apex Type Methods in Summer ’12 and Making your managed package extensible with Apex Plugins

It's all working, except calls to Type.forName('subscriberClassName') from within the managed package always return null for classes outside of the managed package.

It appears that the managed package can't create an instance of the class in the subscriber org. Any ideas?

  • If I use the class name of a default implementation from within the managed package (ManagedNS.Plugings.OpportunityUpdatedDefaultImplementation) the code works fine.
  • The subscriber class and methods are all marked global.
  • Within the subscriber Org I can use anonymous apex to create both the subscriber class and the default implementation defined within the managed package. I have no problems using anonymous apex to invoke the interface methods.
{
    Type t = Type.forName('subscriberClassName');
    ManagedNS.Plugins.IOpportunityUpdated instance = (ManagedNS.Plugins.IOpportunityUpdated)t.newInstance();
    instance.someInterfaceMethod();
}

{
    Type t2 = Type.forName('ManagedNS.Plugings.OpportunityUpdatedDefaultImplementation');
    ManagedNS.Plugins.IOpportunityUpdated instance2 = (ManagedNS.Plugins.IOpportunityUpdated)t2.newInstance();
    instance2.someInterfaceMethod();
}
  • The subscriber Org class subscriberClassName has version settings to the correct version of the beta managed package.

Best Answer

http://success.salesforce.com/issues_view?id=a1p30000000SVY3AAO it's a known issue scheduled to be fixed in next patch release

Related Topic