Multiple updates in a single snippet

  • 1
  • Problem
  • Updated 3 years ago
  • Solved
We have a Javascript snippet where we're trying to update the same object twice (once to clear some fields and then again to set a field and fire a trigger (that requires the other fields to have been cleared)).  Unfortunately, the second update doesn't happen.  The same JS code works in a button in Salesforce, just not in Skuid.

The fields in the first update get changed successfully, but the second update 

Is this something related to configuration? Or is it a limitation with Skuid?

Here's what the code essentially looks like:


    var opp = skuid.model.getModel("Opportunity").getFirstRow();
    var o = new sforce.SObject("Opportunity"); 
    o.Id = opp.Id;
    o.Field_One__c = null;
    o.Field_Two__c = null; 
    
    sforce.connection.update([o]); // This one works
    
    var d = new Date(); 
    o.Send_Date__c = d.toJSON();
    o.Send_Single__c = true;
    
    sforce.connection.update([o]); // This one doesn't work 
Photo of Mike Giles

Mike Giles

  • 230 Points 100 badge 2x thumb

Posted 3 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Is there a reason that you are using the AJAX toolkit to do your updates? This would be a lot easier with the Skuid Action Framework, or with the Skuid JavaScript API if you can't use the Action Framework:

SINGLE ROW SCENARIO

var oppModel = skuid.model.getModel("Opportunity");
oppModel.updateRow(oppModel.getFirstRow(),{
   Field_One__c: null,
   Field_Two__c: null
});
oppModel.save().then(function(result){
    console.log('save finished. Did everything succeed? ' + (result.totalsuccess ? 'yes' : 'no'));
    if (result.totalsuccess) {
        // Do the next update (now that the trigger has finished, ideally)
       oppModel.updateRow(oppModel.getFirstRow(),{
          Send_Date__c: skuid.time.getSFDate(new Date()),
          Send_Single__c: true
       });
       oppModel.save().then(function(result2){
            console.log('2nd save finished!');
       });
    }
});

MULTIPLE SCENARIO

var oppModel = skuid.model.getModel("Opportunity");
var updates = {};
$.each(oppModel.getRows(),function(i,row) {
   updates[row.Id] = {
      Field_One__c: null,
      Field_Two__c: null,
      Send_Date__c: skuid.time.getSFDate(new Date()),
      Send_Single__c: true
   };
});
oppModel.updateRows(updates);
oppModel.save().then(function(result){
    console.log('save finished. Did everything succeed? ' + (result.totalsuccess ? 'yes' : 'no'));
});
(Edited)
Photo of Mike Giles

Mike Giles

  • 230 Points 100 badge 2x thumb
Yeah, we generally have the same logic in Salesforce buttons so we try to keep the code roughly similar on the Skuid side for maintainability (would be very nice if we could just include the javascript SF buttons in Skuid pages to avoid all the duplication, but that's a separate issue).

I can give this approach a try, though your multiple scenario doesn't quite match our use case (we're updating the same Opportunity twice in a row).  Assuming we can call oppModel.Save twice in one snippet, then we should be ok.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
I updated my "single row scenario" snippet example to have a second update and save call immediately after the first one finishes --- that should be totally fine.
Photo of Mike Giles

Mike Giles

  • 230 Points 100 badge 2x thumb
Thank you! This did the trick (once I tracked down a failure due to calling getSFDate instead of getSFDateTime).