Callable Interface
Enables developers to use a common interface to build loosely coupled
integrations between Apex classes or triggers, even for code in separate packages. Agreeing
upon a common interface enables developers from different companies or different departments
to build upon one another’s solutions. Implement this interface to enable the broader
community, which might have different solutions than the ones you had in mind, to extend your
code’s functionality.
Namespace
Usage
To implement the Callable interface, you need to write only one method: call(String action, Map<String, Object> args).
In code that utilizes or tests an implementation of Callable, cast an instance of your type to Callable.
This interface is not intended to replace defining more specific interfaces. Rather, the Callable interface allows integrations in which code from different classes or packages can use common base types.
Callable Methods
The following are methods for Callable.
Callable Example Implementation
This class is an example implementation of the System.Callable
interface.
1public class Extension implements Callable {
2
3 // Actual method
4 String concatStrings(String stringValue) {
5 return stringValue + stringValue;
6 }
7
8 // Actual method
9 Decimal multiplyNumbers(Decimal decimalValue) {
10 return decimalValue * decimalValue;
11 }
12
13 // Dispatch actual methods
14 public Object call(String action, Map<String, Object> args) {
15 switch on action {
16 when 'concatStrings' {
17 return this.concatStrings((String)args.get('stringValue'));
18 }
19 when 'multiplyNumbers' {
20 return this.multiplyNumbers((Decimal)args.get('decimalValue'));
21 }
22 when else {
23 throw new ExtensionMalformedCallException('Method not implemented');
24 }
25 }
26 }
27
28 public class ExtensionMalformedCallException extends Exception {}
29}The following test code illustrates how calling code utilizes the interface to call a
method.
1@IsTest
2private with sharing class ExtensionCaller {
3
4 @IsTest
5 private static void givenConfiguredExtensionWhenCalledThenValidResult() {
6
7 // Given
8 String extensionClass = 'Extension'; // Typically set via configuration
9 Decimal decimalTestValue = 10;
10
11 // When
12 Callable extension =
13 (Callable) Type.forName(extensionClass).newInstance();
14 Decimal result = (Decimal)
15 extension.call('multiplyNumbers', new Map<String, Object> {
16 'decimalValue' => decimalTestValue
17 });
18
19 // Then
20 System.assertEquals(100, result);
21 }
22}