2

I would have thought, given the other questions on the same subject that I would be able to solve this fairly easily, but it does not seem to be the case. I have tried solutions from both the following questions without success -

Managed package cannot see a class from another managed package

A class in one namespace dynamically instantiating an object outside its namespace

I have a managed package which has a helper class called ObjectHelper which is able to instantiate classes given their string name. This package has been installed in another Dev Org. I am trying to call ObjectHelper to instantiate a class created in the Dev Org. Here is the code which tries to instantiate the class (based on one of the answers in the above questions) -

String className = config.Class_Name__c;

Integer index = className.indexOf('.');
if (index != -1) {
    t = Type.forName(className.substring(index), className.substring(index + 1, className.length()));
} else {
    t = Type.forName(null, className);
}

I have also tried the following -

String className = config.Class_Name__c;

t = Type.forName(className);

if (t == null)
    t = Type.forName('', className);

Nothing is working. I have tried to instantiate the class in anonymous code on the Dev Org using the following and it works fine -

String className = config.Class_Name__c
System.debug('TYPE - ' + Type.forName(null, className));
0

I finally figured it out. The class being instantiated needed to have itself and its constructors be global. They were originally set to public. Once the scope was changed the installed package found the type and was able to construct it as necessary using the following code as mentioned above -

String className = config.Class_Name__c;

Integer index = className.indexOf('.');
if (index != -1) {
    t = Type.forName(className.substring(index), className.substring(index + 1, className.length()));
} else {
    t = Type.forName(null, className);
}
| improve this answer | |

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.