I want to use dependency injection to detect if descendant package is installed in addition to inherited package and modify the behaviour of inherited package to depend on class from descendant package.

I use Type.forName() method for that and it works just fine on the scratch org (both namespaced and unpackaged) but it doesn't work in subscriber class.

This is strange, but when I query or get information for custom object from the descendant package I can use local name without a problem. However, when I use Type.forName it throws a null pointer exception even when I try to prepend the shared namespace between packages or use overload version with two parameters, this doesn't help

I tried the following options:

Option 1.

Type.forName('ClassName').newInstance(); // <= Null Pointer error here

Option 2.

Type.forName('Namespace.ClassName').newInstance(); // <= Null Pointer error here as well

Option 3.

Type.forName('Namespace', 'ClassName').newInstance(); // <= Null Pointer error here as well

1 Answer 1


Looks like the only change which has to be made was adding @namespaceAccessible annotation to the ClassName in the descendant package.

public with sharing class ClassName extends AncestorClassName{

My prior understanding was that only classes from the ancestor package need this annotation, but looks like this was a false assumption

You must log in to answer this question.

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