1

I am preparing an un managed package. I need to call a static method in a managed package from the unmanaged package. I went through salesforce documentation on Type class. But I didnt find any way to call a static method.

5

The only way to call dynamic methods is to use instance logic. So you'll need an interface layer here:

public interface StaticMethod { void execute(); }

Then your implementations would look something like:

public class MyManagedMethod implements StaticMethod
{
    public void execute()
    {
        MyManagedClass.doStuff();
    }
}

Then you can dynamically call methods something like:

public void executeStaticMethod(Type methodDefinition)
{
    StaticMethod method = (StaticMethod)methodDefinition.newInstance();
    method.execute();
}
0
0

If you want to call a static method, then you have to call it explicitly and it can't be done via the Type class. If you don't want to introduce a hard dependency to the managed package, then Adrian's answer is definitely the one. It allows you to use alternative implementations for the static method.

If you can live with depending on the managed package, then it's just a matter of correctly using the namespace. If the managed packaged namespace is abc, the class is Utils, and the method is utilMethod(), then you call from your unmanaged package as:

abc.Utils.utilMethod()

This does then mean that your unmanaged package requires that particular managed package to do its job.

If you want to allow for alternative implementations, then you should go with the interfaces method of Adrian (plus a custom setting or custom metadata to say which class to load).

1
  • How can I use Adrian's approach with customsetting to load the class and call a static method? Could you please explain with an example? – Surendranadh Nune Jan 22 '18 at 16:23

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.