How can we populate the variable sr in the line - #[removed][removed](document.getelementsbyname("signed_request")[0].value);
We haven't figured out how to get the signed request (sr) in the visualforce page where we have a canvas app.
We are publishing a canvas event in our external app like:
Sfdc.canvas.client.publish(sr.client, { name : "namespace.eventName", payload : {status : 'Completed'} } );
And first we try to subscribe to the event in the VF page like:
[removed][removed] Sfdc.canvas(function() { Sfdc.canvas.controller.subscribe( { name : ‘namespace.eventName’, onData : function(e) { console.log("Subscribed to custom event ", e); } }); });
but we are getting the error Uncaught TypeError: Cannot read property 'publish' of undefined meaning that controller is undefined
So, we want to subscribe using the Sfdc.canvas.client.subscribe but that method takes two parameters, one of them is the signed request which we don't know how to get in the VF page
Eg:});
Sfdc.canvas(function() { sr = JSON.parse('<%=signedRequestJson%>'); Sfdc.canvas.client.subscribe(sr.client, {name : 'mynamespace.statusChanged', onData : function (event) { console.log("Subscribed to custom event ", event); }} );
So, the question is, how can we populate the variable sr in the line sr = JSON.parse('<%=signedRequestJson%>');?
To populate the variable sr in the line - #[removed][removed](document.getelementsbyname("signed_request")[0].value);
Add onCanvasAppLoad attribute in your canvas as described below:
Add below JS function in your VF page where is the canvas located:
var onCanvasLoad = function(){ alert(document.getElementsByName('signed_request')[0].value.split('.')[1]); };
The document.getElementsByName('signed_request')[0].value.split('.')[1] content is the signed request encoded in base64 format, so that you need to decode it as well.
Here is some useful info regarding Base64 encoding/decoding.