1

I'm trying to enable my managed package to receive a callout from another managed package on the same org. The requirements are that there is no dependency between the packages.

I researched and found that if a package making the call references the method from the package receiving the call, will make them dependant, so I guess that is not the option?

The second thing I found is that this can be achieved using REST callouts. Is this my best option? If I just expose REST methods in my app, will another managed app on the same org be able to access that API then? Are there any limitations I need to be aware of?

Is there any other viable approach?

1 Answer 1

3

Apex includes an interface called Callable that is useful for this case.

You implement a class using a well known name that implements Callable and then other packages can instantiate that class and invoke it via the Callable interface. This avoids the craziness of REST callouts, but keeping a similar level of openness in what is sent and what is received. And no compile-time dependency.

Callers can do this:

Type t = Type.forName('ns.DefaultCallable');
if (t != null) {
    Callable c = (Callable) t.newInstance();
    Object result = c.call('op', new Map<String, Object{...});
    ...
} else {
    ...
}

and you implement this:

global inherited sharing class DefaultCallable implements Callable {

    global Object call(String action, Map<String, Object> args) {
        switch on action {
            // Delegate here to other methods and classes
            ...
        }
    }
}

How the requests and responses are structured is purely represented in documentation. The good news is that Apex has nice syntax for creating lists and maps, and casts can be used so a call can be e.g.:

Contact contact = ...;
Map<String, Object> args = new Map<String, Object>{
    'abc' => new Integer[] {1, 2, 3},
    'contact' => contact
};

Callable c = ...;
Contact updatedContact = (Contact) c.call('op', args);
...
1
  • Thanks Keith, appreciated.
    – Notoc
    Commented Aug 18, 2021 at 8:50

You must log in to answer this question.

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