Swag Level Platform Events Demo
Example subscribing and publishing Platform Events in nodejs using jsforce, humorously to know when swag levels get dangerously low
Demo app created for the Nashville Salesforce TrailheaDX meetup.
Ensure Node.js 8.2.0 or greater is installed on your machine
Create a connected app and enable OAuth Settings
In Salesforce Setup, navigate to Platform Events
Create a new event object named "Swag Level" (
Swag_Level__e) with one custom text field named "Level" (
Checkout this GitHub project locally
In the root folder of the project, create a text file named
.envwith the following properties:
NODE_ENV=development # ========================================== # Salesforce OAuth # ========================================== OAUTH_SALESFORCE_LOGIN_URL=https://login.salesforce.com OAUTH_SALESFORCE_CLIENT_ID=your_connected_app_client_id OAUTH_SALESFORCE_CLIENT_SECRET=your_connected_app_client_secret OAUTH_SALESFORCE_REDIRECT_URI=http://localhost:8080/oauth2/callback # ========================================== # Salesforce # ========================================== SALESFORCE_API_VERSION=40.0
- From command line, navigate to the root folder of the project and run the command
- In your browser, navigate to
http://localhost:8080(you'll be prompted to login with your Salesforce credentials via oauth)
- When the page loads, click the Publish Event link to cause a new
Swag_Level__erecord to be published.
- In your command console you should see nearly immediately a message that the event has been received.
Using the Node.js and Salesforce API is not the only way to subscribe or publish to Platform Events.
You can also subscribe to events via an Apex Trigger configured for the
after insert trigger event on your
as you would for any other sobject.
You can also publish new events similarly to how you would create any other sobject, just insert a new
You can even publish events via Process Builder or Flows since they can insert records too.
Note, although the look and feel of Platform Event objects is like SObjects (that's intentional for easier adoption), they are not data like your SObject records like accounts and contacts. If your regular Salesforce transaction rolls back, any events you've published are not rolled back.