Streaming API in Salesforce with Example | Techila
1250
single,single-post,postid-1250,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Streaming API in Salesforce with Example

streaming-api

Streaming API in Salesforce with Example

Streaming API’s are useful when you want to push the Notifications from Server to Client. It is best suited for applications which keep polling Salesforce for Record Changes. These applications would be benefited using Streaming APIs as it will reduce the unnecessary API calls and processing time of the requests which Return No Data.

 

 Before we start developing let us work with the settings:

       The “Streaming API” permission must be enabled.

       To verify that the “API Enabled” and “Streaming API” permissions are enabled in your organization, from Setup, enter User Interface in the Quick Find box, then select User Interface.

    

Following are steps to do:

Step 1: Create a Push Topic –

1. Open the Developer Console.

2. Click Debug > Open Execute Anonymous Window.

3. In the Enter Apex Code window, paste in the following Apex code, and click Execute.

 

PushTopic pushTopic=new PushTopic();

pushTopic.Name = ‘AccountUpdates’;

pushTopic.Query = ‘SELECT Id, Name,type from Account’;

pushTopic.ApiVersion = 36.0;

pushTopic.NotifyForOperationCreate = true;

pushTopic.NotifyForOperationUpdate = true;

pushTopic.NotifyForOperationUndelete = true;

pushTopic.NotifyForOperationDelete = true;

pushTopic.NotifyForFields = ‘Referenced’;

insert pushTopic;

 

Step 2: Download the Zip and extract the files:   

http://s000.tinyupload.com/index.php?file_id=80570159672625190356

From Setup, enter Static Resources in the Quick Find box, then select Static Resources to add the extracted files with the following names:

 

File Name                      Static Resource Name

cometd.js                        cometd

jquery-1.5.1.js                 jqueryS

json2.js                             json2

jquery.cometd.js            jquery_cometd

 

Step 3: Create Visualforce Page :

Copy and paste the below code  :

<apex:page >
<apex:includeScript value=”{!$Resource.cometd}”/>
<apex:includeScript value=”{!$Resource.jqueryS}”/>
<apex:includeScript value=”{!$Resource.json2}”/>
<apex:includeScript value=”{!$Resource.jquery_cometd}”/>
<script type=”text/javascript”>
13
Example: Visualforce Page Step 2: Create a PushTopic
(function($){
$(document).ready(function() {
// Connect to the CometD endpoint
$.cometd.init({
url:
window.location.protocol+’//’+window.location.hostname+’/cometd/24.0/’,
requestHeaders: { Authorization: ‘OAuth {!$Api.Session_ID}’}
});
// Subscribe to a topic. JSON-encoded update will be returned
// in the callback
$.cometd.subscribe(‘/topic/AccountUpdates’, function(message) {
$(‘#content’).append(‘<p>Notification: ‘ +
‘Channel: ‘ + JSON.stringify(message.channel) + ‘<br>’ +
‘Record name: ‘ + JSON.stringify(message.data.sobject.Name) +
‘<br>’ + ‘ID: ‘ + JSON.stringify(message.data.sobject.Id) +
‘<br>’ + ‘Event type: ‘ + JSON.stringify(message.data.event.type)+
‘<br>’ + ‘Created: ‘ + JSON.stringify(message.data.event.createdDate)
+
‘</p>’);
});
});
})(jQuery)
function disconnect() {
$.cometd.disconnect();
}
window.onbeforeunload = disconnect;
</script>
<body>
<div id=”content”>
<h1>Streaming API Test Page</h1>
<p>This is a demonstration page for Streaming API. Notifications from the
accountUpdates channel will appear here…</p>
</div>
</body>
</apex:page>

Step 4: Open the newly created page. On a different tab create a new account. You will see the notification popping up on the VF page.

Thats all you can do!!!

 

Courtesy: http://www.worldofsalesforce.com/2016/05/streaming-api-in-salesforce-with-example.html

Author: techila

No Comments

Post A Comment