Mixing actions and snippets

So, I have been working on some situations where I am mixing actions from the action framework, and actual snipppets. My issue is along the lines of this one: Block UI and show message until sforce.connection.remoteFunction() call completes

I would imagine that most users would want each action to wait for the prior on to finish. Technically that would make every action a callback inside the previous one. I think that there should at least be an option on every action with something like “Don’t run the next action until this one finishes”. I think that would be a huge help to everyone using the action framework. Currently I have run into many issues with actions running too early.

Hi Moshe,

Have you tried using the Deferred / Promise approach, as described in the post you referenced? 

With all standard actions that are asynchronous, such as “Save Models” and “Requery Models”, the Action Framework does exactly this — it waits until the asynchronous Save / Requery actions are finished before moving on to further Actions, if everything succeeds, or to On-Error Actions, if the Save/Requery has a problem. 

But if you are using Snippets, it’s impossible for Skuid to “automatically” perform this sort of behavior, because Skuid doesn’t know what kinds of asynchronous processing you are doing inside your function — you as the Snippet writer are the one who has to decide at which points in your logic it is “okay” for the Action framework to proceed, and whether to “succeed”, or to “fail” and run On-Error Actions instead. All that Skuid can provide are the tools for telling Skuid to “move on” or to “fail” — which is exactly what the approach in the article above is providing. Your Snippet returns a Deferred Promise, and then you call either resolve() or reject() within the appropriate Callbacks in your code. If you call resolve(), Skuid knows to proceed with further actions, or if you call reject(), Skuid knows to run On-Error Actions.

OK perfect Zach, I was unsure of how exactly the whole Deffered/Promise thing worked (jQuery beginner). I also thought that it might only work with Irvins situation with a remote action. So basically if I’m writing a snippet that runs in middle of an action sequence, I can always do this:

var dfd = $.Deferred();<br>//do cool logic here<br>// assuming I have no on-error actions to attach a Callback to...<br>dfd.resolve();&nbsp;<br>return dfd.promise(); 

Will this work?

Yes — you can apply the Deferred/Promise approach to any asynchronous scenario. Here’s some pseudo-code for the idea:

var dfd = $.Deferred();<br>someAsynchronousFunction({<br>&nbsp; &nbsp; onSuccess: function(result) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Run some logic to determine whether everything's cool<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (result.isGoodToGo) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Tell Skuid that we're okay to move on to the next action!<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dfd.resolve();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Tell Skuid to run on-error actions<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dfd.reject();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; },<br>&nbsp; &nbsp; onError: function(result){<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Tell Skuid to run on-error actions<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dfd.reject();<br>&nbsp; &nbsp; }<br>});<br>return dfd.promise();