function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
halopezhalopez 

Salesforce Canvas: synchronous method calls

Recently I have been playing around with Canvas, a technology that seems quite promising for the integration of third party applications within salesforce.

 

The canvas JavaScript API provides ways of making cross domain http request at an asynchronous level. I am interested in having synchronous method calls, and I don´t know whether that can be achieved. 

 

The following is the JavaScript example (combining the two examples given in the manual)

 

 

        function displayChatterUsers() {
          var NumberUsers = 0;
          // Reference the Chatter user's URL from Context.Links object.
          var chatterUsersUrl = canvasContext.context.links.chatterUsersUrl;

          // Make an XHR call back to salesforce through the supplied browser proxy.
          Sfdc.canvas.client.ajax(chatterUsersUrl,
            {
              token: canvasContext.oauthToken,
              success: function (data) {
                // Make sure the status code is OK.
                if (data.status === 200) {
                  numberUsers = data.payload.users.length;
                  // Alert with how many Chatter users were returned.
                  alert( "Got back " + numberUsers +
                    " users"); // Returned 2 users
                }
              }
            });
          alert( "Test for the number of users is " + numberUsers );
            var url = canvasContext.context.links.chatterFeedsUrl + "/news/" + canvasContext.context.user.userId + "/feed-items";
            var body = { body: { messageSegments: [{ type: "Text", text: "The number of users is " + numberUsers}]} };
          Sfdc.canvas.client.ajax( url,
            {
              token: canvasContext.oauthToken,
              method: 'POST',
              contentType: "application/json",
              data: JSON.stringify( body ),
              success: function( data ) {
                if ( 201 === data.status ) {
                  alert( "Success" );
                }
              }
            } );
        }

 When running the above mentioned example, you could see that the Sfdc.canvas.client.ajax get delayed in its execution, disallowing me from use of the results of the method call in outer levels of nesting (for instance, in the top level alert()).

I tried adding async:true as part of the values for the method call (as one might infer from the canvas-all.js specification) with no luck.

 

Any hints are greatly appreciated.

 

Andy

MattreyMattrey

Andy, the success element you're specifying is the callback for when the server returns its response. When you call ajax() as specified, it will return as soon as the call is made and the success function will run when the response is returned. 

 

If you want it to wait until the response is available before returning, you need to add async:false to your request structure like this:

 

Sfdc.canvas.client.ajax(chatterUsersUrl,
{token : sr.oauthToken,
async:false,
success : function(data){
// Make sure the status code is OK.
if (data.status === 200) {
// Alert with how many Chatter users were returned.
alert("Got back " + data.payload.users.length +
" users");
}
}});

 

I wouldn't recommend that as you will be blocking anything from happening until the server responds - better to stick what you need to depend on the result in the callback. If you want to get really fancy, google javascript promises 

jhurstjhurst

Additionally, you could force synchronous behavior by doing a server to server call, rather than using the Canvas SDK (ie - make the call from your app directly to the salesforce.com servers, rather than using the AJAX method in the SDK).

 

Hope this helps.

Jay