Rollbacks when saving records via Javascript

  • 1
  • Question
  • Updated 3 years ago
  • Answered
When using the action framework to save models, it is possible to select multiple models to be saved. There's an option to specify that the entire operation should be rolled back if there are any errors. This can be used to prevent records being created if later operations fail.

For example: imagine a page where you enter a new account, and new contacts related to that new account in one page. You set up an action that saves both models, and the Account is saved, and the Contact is saved and linked to the account. Let's say that for some reason the Contact save fails - let's say a validation rule fires. If the rollback option is checked, that will prevent the Account from being created as well.

Is there a way to do this when using the Javascript API? I may be looking in the wrong place, but the closest I can see is skuid.model.save() which takes a function that is called when the save finishes. The problem with this is that the function is only told what succeeded and what failed; it doesn't roll back. Feasibly, at this point you could try and delete records, but it's possible that the user has create/edit but does not have delete for the object that needs to be deleted. 

Anybody had any thoughts about this?
Photo of Gary

Gary

  • 1,518 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Irvin Waldman

Irvin Waldman, Champion

  • 9,006 Points 5k badge 2x thumb
Maybe its an undocumented option?
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Well, it looks as though the model does store the original values that the model started with. So, in theory, if there is any failure you could loop through these entries in order to restore the rows and resave them.



I, as well, would use the skuid.model.save() function.

                    $.when(skuid.model.save(models))
                        .done(function(){
                            // loop through each model and copy all originals back into data array
                            dfd.resolve();
                        })
                        .fail(function(){
                            dfd.reject();
                        });
                    
                    return dfd.promise();
Photo of Gary

Gary

  • 1,518 Points 1k badge 2x thumb
Thanks, but that could still leave the database with records you may not want. For now, we're preparing the models in Javascript and then using an action to save them afterwards.
Photo of Salesforce Dev007

Salesforce Dev007

  • 308 Points 250 badge 2x thumb
I am currently in a similar situation where I need to update a field to 'shipped' but it requires all items to have a quantity entered first. Both these things need to be saved in one transaction otherwise it should save neither. Could we call a controller class in apex to handle this logic? Can someone point me to the right direction?
(Edited)
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
If you use the action framework "Save Models" feature,  there is a rollback option.  We have worked really hard to ensure that if a secondary validation fails,  the whole operation is rolled back  (If items records don't have quantity,  order record 'shipped' value can't be saved).   Please make sure you are on a current version of skuid (from skuidify.com/skuidreleases)  and if you are seeing unexpected results, please let us know.. 



We love Apex as much as the next guy.  But if you don't need it.. why would you go there? 
Photo of Salesforce Dev007

Salesforce Dev007

  • 308 Points 250 badge 2x thumb
Hey Rob, I am already doing this step but the problem is validating the data on server side. Can't seem to find a way to validate it correctly on server side using validation rules since the changes are inter-dependent between the two objects i.e. the Shipping object needs to have an actual date entered while the shipping items have to have the quantities entered in order to set the Shipping object status to 'shipped'. Both cannot be validated using the validation rules since we do not know what order the models are saved and not sure if there is way to get new updated value in the validation rules as I have already tried this. 

Another alternative I thought was to use webservice methods to do this but then I would have to wrap up all the changes in a custom object in javascript snippet to send it to the server side. 

Currently I am thinking of taking the second path since it would allow the data to be validated on both client and server side unless there is another way to handle this scenario. 

Thanks.
Photo of Gary

Gary

  • 1,518 Points 1k badge 2x thumb
Sukhpreet, I believe the order the models are save is deterministic - they should save in the order they are listed in the Models section. So if Shipping comes before Shipping Items, you shouldn't have an issue. I think you have a "Required" flag on the Date field on Shipping (or validation rule if it's not always required), the same for the quantity on Shipping Item, and then a workflow or process to update the parent Shipping record's status.

Other options would be a trigger(s), or possibly an InvocableAction(s) - I like the second as it can be called from a Process built in Process Builder, but also from within a Skuid page. Obviously, anything critical done on a client should be checked on the server side as well.