Can I update a 3rd party web serivce during Skuid submit?

  • 1
  • Question
  • Updated 5 years ago
Is there a way to intercept a Skuid form submission in order to update a 3rd party service via web service? Example:

1. Update the account name for one or more account records
2. Click "Save"
3. All data for the updated record(s) is sent to 3rd party WS via AJAX POST
4. Response from WS is received
5. (If success) Request continues and updates SF record normally.
6. (BONUS) If SF update fails, WS is called and resets the previously updated records to their previous values
Photo of Alain Nisam

Alain Nisam

  • 70 Points

Posted 5 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 48,984 Points 20k badge 2x thumb
There are a couple of ways to do this.

The easiest way would be to have a custom Page Title button called "Save" that runs a JavaScript Snippet. The Snippet would do exactly what you say in Steps 3-6, something like this:



var params = arguments[0],
model = params.model,
row = params.row,
$ = skuid.$;

// Do the Web Service callout
/*

var okayToProceed = true;

var dfd = $.ajax({
...
});
*/

// Once the Web Service callout is all finished,
// assuming that there were no errors during the callout,
// proceed with saving the Skuid Model
// specified in the Page Title component
// (or other Models if you'd like)
$.when(dfd).then(function(){

if (!okayToProceed) return false;

model.save({callback:function(result){
if (result.totalsuccess){
// Success! Normal Skuid post-save-stuff will happen.
} else {
// Roll-back the Web Service changes
}
}});
});



Another way would be to actually intercept skuid.model.save, the core method that handles Skuid save operations, by doing something like this (this is illustrative only, not a fully-fleshed out example) in a Skuid Inline JavaScript Resource:



(function(skuid){

var $ = skuid.$;
var originalSave = skuid.model.save;
skuid.model.save = function(arrayOfModels,options){

var dfd = $.Deferred();

$.each(arrayOfModels,function(i,model){
// Perform Web Service logic.
// When totally done,
//resolve our Deferred so that Skuid Save can proceed
if (i === arrayOfModels.length-1){
dfd.resolve();
}
});

// When all of our Web Service callouts are done,
// carry on with the original Skuid behavior
$.when(dfd).then(function(){
originalSave(arrayOfModels,options);
});

};

})(skuid);

Photo of Jacob Flatter

Jacob Flatter

  • 338 Points 250 badge 2x thumb
How does the $.ajax post get around the cross origin limitation within the browser?

I am getting this error in the console: "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://skuid.na15.visual.force.com' is therefore not allowed access."

Jacob
Photo of JD Bell

JD Bell, Senior Product Engineer

  • 2,996 Points 2k badge 2x thumb
Salesforce does not allow XSS. To make an AJAX call to an external site, you must use the AJAX Toolkit. Note that each call using the AJAX Toolkit counts against your API Usage Limits.
Photo of Jacob Flatter

Jacob Flatter

  • 338 Points 250 badge 2x thumb
This will let me get information from an external API, and then use Skuid's js to insert data back into model?

I don't see anything other than Sobject CRUD in those docs.

Jacob
Photo of Zach McElrath

Zach McElrath, Employee

  • 48,984 Points 20k badge 2x thumb
Go to this doc and scroll down to "AJAX Proxy".

You need to use sforce.connection.remoteFunction().

The JavaScript file "connection.js" is pre-included with all Skuid Pages, so you DON'T need to include it yourself.
(Edited)
Photo of Jacob Flatter

Jacob Flatter

  • 338 Points 250 badge 2x thumb
Thank you sir,

Have a good Weekend,

Jacob