Durable Streaming API with MuleSoft Salesforce Connector

November 15 2016

3 comments 0
durable-streaming-api-with-mulesoft-salesforce-connector

introduced the Streaming API with its Spring ‘16 release (API v36). It was a neat feature since Salesforce users don’t have to poll or periodically call Salesforce to check any updates in their objects. One important limitation with API v36 was that the Streaming API (API v36) doesn’t guarantee durability and reliable delivery of notifications. However, Salesforce added message reliability and message durability in its API v37. We are excited to announce the release of Salesforce Connector v8.0.0 which supports the Durable Streaming API. (The Salesforce Connector supporting the API v38 will be released soon.)

In this blog post, I’d like to walk you through a simple demo app which shows how to use Streaming API with Salesforce Connector.

Before you start with the demo app, please make sure you have the Salesforce Connector v8.0.0 downloaded from the Exchange.

screen-shot-2016-10-27-at-2-04-38-pm

After you install the Salesforce Connector, copy and paste the following xml in your mule project, and you will see the two Mule flows in the project.

screen-shot-2016-10-27-at-2-11-22-pm

Once you load the example, let’s configure the connector. To configure the Salesforce Connector, go to Global Elements, and find “Salesforce.” After selecting “Salesforce,” click “Edit”.

screen-shot-2016-10-27-at-2-13-01-pm

screen-shot-2016-10-27-at-2-13-09-pm

 

In the “Salesforce: Basic Configuration”, you can specify the configuration for Salesforce. You could directly add your Salesforce information in the configuration, but we recommend using the properties file to add your configuration information. In mule-app.properties, please configure the following properties based on your Salesforce instance.

sfdc_username=
sfdc_password=
sfdc_token=

Now, it is time to create a Custom Object and a PushTopic which the Salesforce Connector will listen to. For this demo app, I just follow the Salesforce instruction to create a Custom Object called Invoice Statement and create a PushTopic called InvoiceStatementUpdates by following the Salesforce instruction. Now you are ready to subscribe the InvoiceStatementUpdates with Salesforce Connector. Before you run the mule application, please make sure you change the initial state of the first flow from stopped to started.

Go to Salesforce and add a new invoice to your Invoice Statement object.

screen-shot-2016-10-27-at-2-26-07-pm

As soon as you create a new invoice, you will see the following message logged in Studio. (Your result might be different.)

INFO  2016-10-27 14:26:15,831 [[salesforce_streaming].salesforce_streaming_topic.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Invoice for October, Id=a021a00000C9EdYAAV, Status__c=Open, Name=INV-0003}

Let’s change the operation from Subscribe topic to Replay topic, and select All in the Replay Option. Salesforce stores events for 24 hours, so All in the Replay Option will show all events created over the past 24 hours. I received the following events from Salesforce. You will see different results based on the events you create.

INFO  2016-10-27 14:33:10,690 [[salesforce_streaming].salesforce_streaming_topic.stage1.06] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Invoice for October, Id=a021a00000C9EdYAAV, Status__c=Open, Name=INV-0003}

INFO  2016-10-27 14:33:10,690 [[salesforce_streaming].salesforce_streaming_topic.stage1.04] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Send invoice updated, Id=a021a00000C9EaEAAV, Status__c=Open, Name=INV-0002}

INFO  2016-10-27 14:33:10,690 [[salesforce_streaming].salesforce_streaming_topic.stage1.05] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Test Invoice statement updated, Id=a021a00000C9Ea9AAF, Status__c=Open, Name=INV-0001}

INFO  2016-10-27 14:33:10,690 [[salesforce_streaming].salesforce_streaming_topic.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Test Invoice statement, Id=a021a00000C9Ea9AAF, Status__c=Open, Name=INV-0001}

INFO  2016-10-27 14:33:10,690 [[salesforce_streaming].salesforce_streaming_topic.stage1.03] org.mule.api.processor.LoggerMessageProcessor: {Description__c=Send invoice, Id=a021a00000C9EaEAAV, Status__c=Open, Name=INV-0002}

Salesforce Streaming API also supports capabilities of subscribing to a streaming channel and replaying it. Change the status of the first flow from started to stopped and the status of the second flow from stopped to started, and restart the mule app.

For this demo, I created a Streaming Channel called /u/notifications/ExampleUserChannel and generated events with Salesforce Workbench to /u/notifications/ExampleUserChannel (My channel’s id is 0M61a000000PAsXCAW, but yours might be different.) by following the Salesforce Instruction.

screen-shot-2016-10-27-at-2-49-21-pm

As soon as I generated if the event above, I received the following result in Studio.

INFO  2016-10-27 14:49:40,698 [[salesforce_streaming].salesforce_streaming_channel.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {payload=Connect Anything, Change Everything!, event={createdDate=2016-10-27T17:49:38.972Z, replayId=2}}

Let’s change the operation from Subscribe streaming channel to Replay streaming channel.

screen-shot-2016-10-27-at-2-54-38-pm

As you can see below, I received two events published to /u/notifications/ExampleUserChannel over the past 24 hours.

INFO  2016-10-27 14:55:55,206 [[salesforce_streaming].salesforce_streaming_channel.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {payload=Broadcast message to all subscribers, event={createdDate=2016-10-27T16:44:05.672Z, replayId=1}}

INFO  2016-10-27 14:55:55,206 [[salesforce_streaming].salesforce_streaming_channel.stage1.03] org.mule.api.processor.LoggerMessageProcessor: {payload=Connect Anything, Change Everything!, event={createdDate=2016-10-27T17:49:38.972Z, replayId=2}}

For new users, try the above example to get started, and for others, please share with us how you are planning to use the Durable Streaming API with the Salesforce Connector! Also, explore the Anypoint Exchange to see other resources you can leverage today.


 


We'd love to hear your opinion on this post


3 Responses to “Durable Streaming API with MuleSoft Salesforce Connector”

  1. Hi, when I subscribe to the topic I get an SObject id, how can I use this id to update back in salesforce ? I found some examples but non of them work. For example updating a text field in Event object

  2. I am not able to see xml file in this blog.
    Please share or update the blog.

    Thanks
    Akash Ranglani