This answer: Managed Package Integration without Extensions or Dependencies is now 4 years old and I was curious if it is still the only way to accomplish the following goal:
- Main Package
- Extension Package - May or May Not be installed along with the main package
- In main package need to determine if Extension is installed
- If installed run method in extension package passing in parameters
- No dependency can be created as the extension is not required but if present it must run at a specified point
So basically need to check if extension is installed and if so run a parameterized method without creating dependencies.
System.Type does not allow for the execution of methods or the passing of parameters AFAIK. I do not see any other way to accomplish this.
Granted, the previous answer is still applicable and does not consume much CPU time at all. Just curious if it is still the only and preferred way of doing this.
In the simplest form the following is what I currently have:
Main Package (Batch Apex)
System.Type apiMethod = Type.forName('ns', 'Class.Method');
If(apiMethod != null){
Object apiCall = JSON.deserialize(
'{"param1":null,"param2":"12345"}',
apiMethod
);
}
Extension Package
global class{
global Id param1 {get;set;}
global string param2 {get;set;}
global method(){
if(string.isBlank(param2) || param1 == null)
throw new ex_Exception('Invalid Parameters');
secondMethod(param1,param2);
}
}
global static void secondMethod(Id param1, String param2) {
..Do something...
}
}
Please ignore naming conventions as for illustration only. This works and does not create a dependency in either package.
Type.forName
to see if the implementation is valid. With an interface, you wouldn't have to shove all the logic into a constructor but could rather have some methods to work with.Type implementation = Type.forName('ImplementingClass'); if (implementation != null) { Interface instance = (Interface)implementation.newInstance(); instance.doStuff(); }
You should be able to call that from the main package I would think.