Create a new Planform Event Object and give it a name App Metadata (App_Metadata__e) which will also be the name of the topic. Create some fields you that you would like to listen to. In Apex add the following event
Build event payload
List<App_Metadata.BPUpdateAccounts> updatedAccountList = new List<App_Metadata.BPUpdateAccounts>(); App_Metadata.BPUpdateAccounts updatedAccount = new App_Metadata.BPUpdateAccounts(); updatedAccount.setId(financialAccount.Id); updatedAccount.add(updatedAccount); new App_Rest_Events(new App_Rest_Events.Event(UserInfo.getUserId(), App_Rest_Events.EventType.UPDATE_ACCOUNT, updatedAccountList)).publishToEventBus();
Publish Event to eventBus
public void publishToEventBus(){ Object eventPayload = buildEventPayload(); EventBus.publish(new App_Metadata__e(Metadata_Json__c=JSON.serialize(eventPayload))); }
Now let’s create our event listen to subscript to the topic and consume the messages from the EventBus.
Add the emp-connector jar to project
<repositories> <repository> <id>emp-connector</id> <url>${project.basedir}/lib/emp-connector-0.0.1-SNAPSHOT-phat.jar</url> </repository> </repositories> .... <dependency> <groupId>com.salesforce.conduit</groupId> <artifactId>emp-connector</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
Create TopicSubscription to your event table by name
package com.app.salesforce.eventbus; import com.salesforce.emp.connector.BayeuxParameters; import com.salesforce.emp.connector.EmpConnector; import com.salesforce.emp.connector.TopicSubscription; import java.net.URL; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import static com.salesforce.emp.connector.LoginHelper.login; /** * Created by thysmichels on 5/15/17. */ public class SalesforceEventBus { private static final String SALESFORCE_USERNAME="salesforceUsername"; private static final String SALESFORCE_PASSWORD="salesforcePassword"; public static void main(String[] argv) throws Exception { long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; BayeuxParameters params; BayeuxParameters custom = getBayeuxParamWithSpecifiedAPIVersion("39.0"); try { params = login(new URL("https://test.salesforce.com"),SALESFORCE_USERNAME, SALESFORCE_PASSWORD, custom); } catch (Exception e) { e.printStackTrace(System.err); System.exit(1); throw e; } Consumer<Map<String, Object>> consumer = event -> System.out.println(String.format("Received:\n%s", event)); EmpConnector connector = new EmpConnector(params); connector.start().get(5, TimeUnit.SECONDS); TopicSubscription subscription = connector.subscribe("/event/App_Metadata__e", replayFrom, consumer).get(5, TimeUnit.SECONDS); System.out.println(String.format("Subscribed: %s", subscription)); } private static BayeuxParameters getBayeuxParamWithSpecifiedAPIVersion(String apiVersion) { BayeuxParameters params = new BayeuxParameters() { @Override public String version() { return apiVersion; } @Override public String bearerToken() { return null; } }; return params; } }
Topic receives response from subscribed channel
Subscribed: Subscription [/event/App_Metadata__e:-2] Received: {schema=D6-eSgLDrahnNjuNI52XAg, payload={CreatedById=00521000000UcF0, CreatedDate=2017-05-15T18:35:10Z, Metadata_Json__c="{\"accounts\":[{\"id\":\"28706236\",\"providerAccountId\":14736,\"sfId\":\"a0721000001A6fOAAS\"}, {\"id\":\"YL1111\",\"providerAccountId\":1466,\"sfId\":\"a0721000001A6fPAAS\"}],\"event\":\"UPDATE_GOAL\",\"linkAccounts\":null, \"financialAccountMap\":null,\"goals\":[{\"id\":\"a0821000000OVPUAA4\",\"linkedAccounts\":[\"a0721000001ADqkAAG\"], \"status\":\"In Progress\"},{\"id\":\"a0821000000OVPZAA4\",\"linkedAccounts\":null,\"status\":\"In Progress\"},{\"id\":\"a0821000000OVPeAAO\",\"linkedAccounts\":[\"a0721000001ADqaAAG\"],\"status\":\"In Progress\"}], \"linkUserContext\":{\"accountList\":null,\"additionalProperties\":null,\"deleteAccountList\":null,\"email\":\"blah@email.com\", \"password\":\"p@assw0rd\", \"providerAccountIds\":null,\"sfClientId\":\"0033600000NO1QjAAL\",\"sfHouseholdId\":\"0013600000VNXflAAH\",\"sfUserId\":\"00521000000UcF0AAK\", \"type\":\"YL\",\"username\":\"YL00F0A\"},\"priority\":5,\"providerAccounts\":null, \"updatedAccounts\":null,\"updatedGoals\":[{\"id\":\"a0821000000OVPZAA4\",\"linkedAccounts\":null}],\"mapKeys\":{\"event\":\"event\",\"priority\":\"priority\",\"linkUserContext\":\"linkUserContext\",\"goals\":\"goals\", \"accounts\":\"accounts\",\"providerAccounts\":\"providerAccounts\",\"updatedAccounts\":\"updatedAccounts\",\"updatedGoals\": \"updatedGoals\",\"goalFinancialAccountMap\":\"goalFinancialAccountMap\",\"fastlinkAccounts\":\"fastlinkAccounts\"}, \"mapKeysFlag\":true,\"serializeNulls\":false}"}, event={replayId=1}}