9

I am struggling to understand how pass a value back to a VF page that contains a Canvas App. If I understand the documentation correctly I need to create a publish event in my app, so I have the following script in my page:

<script type="text/javascript">
        function sendValue(someValue) { 
         Sfdc.canvas.controller.publish({
             name: 'myns.sendVal', 
             payload : {value : someValue}
             }); 
         };
</script>

On the VF side I have to subscribe

<apex:canvasApp developerName="myApp" height="1000px" width="1000px"/>         
<script>
    Sfdc.canvas(function() {  
     sr = JSON.parse('<%=signedRequestJson%>');
     Sfdc.canvas.client.subscribe(sr.client,
            {name : 'myns.sendVal', 
             onData : function (data) {                 
                 alert(data.value);
             }
            });
           });                                         
</script>

This is what I have pieced together from the examples I have seen but it does not work. Most likely because all the examples where about doing this in reverse.

  • Frank - I reverted your edits in this question, and moved your answer with the correct code to the answer. Thanks for following up! – metadaddy Sep 10 '14 at 20:57
5

I got it working. The app side is a .NET site where I validate the signedRequest and save the Json to the hidden variable. My problem was that I was using the signedRequest and not the decoded Json. On the VF page I was missing the scripts for the controller and connection. Here is the correct code:

<script type="text/javascript" defer="defer">
    var signedRequest = document.getElementById("<%= hdnSignedRequest.ClientID %>").value;

    function SendValue(pValue) {
        sr = JSON.parse(signedRequest);
        Sfdc.canvas.client.publish(sr.client, { 
            name: 'myns.sendVal', 
            payload: { value : pValue} });
    }
</script>

On the VF side I have to subscribe

<script type="text/javascript" src="/canvas/sdk/js/31.0/controller.js"></script>       
<script type="text/javascript" src="/soap/ajax/31.0/connection.js"/>

<apex:canvasApp developerName="myApp" height="1000px" width="1000px"/>

<script>
    Sfdc.canvas(function() {  
     Sfdc.canvas.controller.subscribe(
            {name : 'myns.sendVal', 
             onData : function (data) {                 
                  var sourceId = data.value;
                    var query = "SELECT ID FROM sObject WHERE External_Id__c = '" + sourceId + "' ";
                    var result = sforce.connection.query(query);
                    var records = result.getArray("records"); 
                    if (records.length > 0) {
                        var sObj= records[0];                        
                        window.location = "/" + sObj.Id;
                     }
                     else { alert("Selected external id does not exist in SalesForce."); }  
             }
            });
           });                                         
</script>
  • Frank - you can mark this answer as accepted to clearly show that it fixes the problem - thanks! – metadaddy Sep 10 '14 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.