Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Subscribing to a Canvas App Event

Subscribing to a Custom Event

The following code example shows how to call the subscribe method to subscribe to a canvas app event.

1// Subscribe to a custom event.
2Sfdc.canvas(function() {
3    sr = JSON.parse('<%=signedRequestJson%>');
4    Sfdc.canvas.client.subscribe(sr.client,
5        {name : 'mynamespace.statusChanged', onData : function (event) {
6            console.log("Subscribed to custom event ", event);
7        }}
8    );
9});

Subscribing to Multiple Custom Events

The following code example shows how to call the subscribe method to subscribe to multiple canvas app events. The events you subscribe to can be in different namespaces or might not have a namespace. When a canvas app subscribes to an event, it creates an association between an event (in the other canvas app) and a function (in the subscribing canvas app).

1// Subscribe to multiple events.
2Sfdc.canvas(function() {
3    sr = JSON.parse('<%=signedRequestJson%>');
4    Sfdc.canvas.client.subscribe(sr.client, [
5        {name : 'mynamespace.statusChanged', onData : handler1},
6        {name : 'anothernamespace.tripCancelled', onData : handler2},
7    ]);
8});

Using the travel and expense and approval canvas app examples, your approvals canvas app has two functions: handler1 and handler2. That canvas app then subscribes to two events in the travel and expense canvas app: mynamespace.statusChanged and mynamespace.tripCancelled. When the mynamespace.statusChanged event is received by the approvals app, function handler1 is called. When the anothernamespace.tripCancelled event is received by the approvals app, function handler2 is called.