Orchestrate Messages Between Packages in Salesforce

Ben Butler
Oct 22, 2021

--

The “Core Package” in Salesforce is a shared dependency of two or more other packages. The core package is also core to decoupling communication between packages.

Because unrelated packages can access the Core Package, the Core can be used to orchestrate messages between them using interfaces. Start by abstracting an orchestrator

public class ClosingEventOrchestrator {
public static void orchestrate(List<Opportunity> opps){
for (ClosingEventConsumerSubscription__mdt flow : [SELECT ApexClass__c FROM ClosingEventConsumerSubscription__mdt ORDER BY Order__c]){
IClosingEventConsumer consumer = (IClosingEventConsumer)Type.forName(flow.ApexClass__c).newInstance();
consumer.consume(opps);
}
}
}

The orchestrator class handles a Closing event. Now the classes that want to consume the event decide to be tied to it, not the classes that publish the event. This is done with an interface class.

public interface IClosingEventConsumer {
void consume(List<Opportunity> opps);
}

Custom metadata enables a configurable separation and decoupling between the packages.

--

--