10

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.

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

1 Answer 1

6

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>
2
  • Frank - you can mark this answer as accepted to clearly show that it fixes the problem - thanks!
    – metadaddy
    Commented Sep 10, 2014 at 20:58
  • Thanks you saved my day! Commented Oct 27, 2020 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .