Save model using Snippet

I have entered data into fields using the Skuid UI, and instead of using the Skuid Save button I would like to use a custom snippet to save and get the Id that is created, I tried the below piece of code but it didn’t work, can anyone help, thanks


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

//get models New Quote Model to save
var QuoteWizardStep3Model = skuid.model.getModel(‘QuoteWizardStep3’);
skuid.model.save([

   QuoteWizardStep3Model

],{callback: function(result){


   if (result.totalsuccess){

      r = m.getFirstRow();

       alert('New Quote Id: ’ + r.Id); // should be a real SF Id now

   }
  
   else {

      // There was a problem. Let’s see what went wrong.
      alert('Error: ’ + result.insertResults[0]);
      console.log(result.insertResults[0]);

   }

}});

Hi Eddie,

I don’t think you defined the “r” and “m” variables you’re using in your callback.

try changing this line

r = m.getFirstRow();


to this.

var r = QuoteWizardStep3Model.getFirstRow();

Why not just save the model and set another model for the same object called “CurrentYourObjectNameHere” with a condition of Id = nothing and filterable and default off.



That way you can reference it any which way you like.

var params = arguments[0],&nbsp; &nbsp; $ = skuid.$;<br>//get models New Quote Model to save<br>//your model variable wasn't called "m"...<br>var m = skuid.model.getModel('QuoteWizardStep3');<br>r = m.getFirstRow();<br>skuid.model.save({callback:function(result){<br>if(result.totalsuccess){<br>alert('New Quote Id: ' + r.Id); // should be a real SF Id now<br>}else{<br>// I have no idea what insert results are but if it works for you...<br>// There was a problem. Let's see what went wrong.<br>&nbsp; &nbsp; &nbsp; alert('Error: ' + result.insertResults[0]);<br>&nbsp; &nbsp; &nbsp; console.log(result.insertResults[0]);<br>}<br>}});

Wow everyone is either really bored or really fast today :slight_smile:

hi I just figured out what was wrong, I used the model.map() method and corrected an error and its working fine now as below:

var $ = skuid.$;
// Get references to our Models
var models = skuid.model.map();
var QuoteWizardStep3Model = models.QuoteWizardStep3;


skuid.model.save([

   QuoteWizardStep3Model

],{callback: function(result){


   if (result.totalsuccess){

      r = QuoteWizardStep3Model.getFirstRow();

       alert('New Quote Id: ’ + r.Id); // should be a real SF Id now

   }
  
   else {

      // There was a problem. Let’s see what went wrong.
      alert('Error: ’ + result.insertResults[0]);
      console.log(result.insertResults[0]);

   }

}});

I really really want to learn everything. Trying to answer every question keeps pushing my envelope. Non-stop new capabilities and a ton of fun!

Pat,  you are seriously working toward becoming the next community champion.  Thanks a lot for your assistance to other Skuid users.  You are making the Skuid community a better place for us all. 

The above snippet Saves the Model ok, but the model but I have Quote__c attached to 2 models:
QuoteWizardStep3 and Quote

QuoteWizardStep3 is for creating new quotes so once I have saved this model, and I start a new quote I don’t want any of the old values being displayed in any of the fields.
Quote needs to be refreshed to show the new quote that was created, and show changes to the other quotes

I tried this to refresh the Quote model but it doesn’t work:
//Refresh the Quote Object fields
            var QuoteModel = skuid.model.getModel(‘Quote’);
            $.each(QuoteModel.registeredItems,function(){
                this.refreshFields();
            });

Can anyone help, thanks


So you have a wizard that is used to create a new quote and then just want to display that quote?

1. Button Action: Run Multiple Actions
2. Set Actions
  Save Model - QuoteWizardStep3
  Set and Activate Value of Model Condition - Quote - {{Id}}
  Query Model - Quote

I am saving the Quote in a Javascript Snippet below:
In the Snippet if result.totalsuccess = true I want to refresh the Quote Model on the page and start with a fresh new QuoteWizardStep3 Model (This Model should load no data, as its just used to create a new quote) although once I Save QuoteWizardStep3 Model and try to create a new Quote the data is still in the fields.

So Quote Model needs refreshed and QuoteWizardStep3 needs to create a new quote, and not have the previous data. This all needs be accomplished in the Snippet.

var $ = skuid.$;
// Get references to our Models
var models = skuid.model.map();
var QuoteWizardStep3Model = models.QuoteWizardStep3;


skuid.model.save([

   QuoteWizardStep3Model

],{callback: function(result){


   if (result.totalsuccess){

      r = QuoteWizardStep3Model.getFirstRow();

       alert('New Quote Id: ’ + r.Id); // should be a real SF Id now

   }
  
   else {

      // There was a problem. Let’s see what went wrong.
      alert('Error: ’ + result.insertResults[0]);
      console.log(result.insertResults[0]);

   }

}});





Here’s what’s going on. Skuid uses a slightly kludge way of using models for data entry, by not loading the model on page load, and creating a default row for the model. If you try calling updateData() on a model, to update it, that re-runs the SOQL query that the model is based on. Since your model probably has no conditions if you called update data, you would get the first random Quote that the SOQL query picked up in your model. To fix this you will have to either

1) update the all of the fields you are using on the model to null or blank strings (kludgiest)
2) set a condition on the model that is impossible to be fulfilled (kludgier)
3) the right way to do this, which I honestly don’t know, but I’m sure Rob does

You also want to set up a condition on the quote model where Id = (blank) filterable default off. Then reference this condition and pass in the value from the saved QuoteWizardStep3Model to update the Quote model. Like so:  

var $ = skuid.$;// Get references to our Models<br>var models = skuid.model.map();<br>var QuoteWizardStep3Model = models.QuoteWizardStep3;<br>// get a reference to the quote model<br>var Quote = models.Quote;<br>//get a reference to the Id condition<br>var quoteCondition = quote.getConditionByName('MyConditionName');<br>skuid.model.save(
[QuoteWizardStep3Model]
,{callback: function(result){<br>&nbsp; &nbsp; if (result.totalsuccess){<br>&nbsp; &nbsp; &nbsp; &nbsp; r = QuoteWizardStep3Model.getFirstRow();<br>&nbsp; &nbsp; &nbsp; &nbsp; alert('New Quote Id: ' + r.Id); // should be a real SF Id now<br>//set the condition<br>quote.setCondition(quoteCondition,r.Id);<br>//update the models<br> &nbsp; &nbsp;skuid.model.updateData(
[Quote,QuoteWizardStep3Model]
);<br>&nbsp; &nbsp; } else {<br>&nbsp; &nbsp; &nbsp; &nbsp; // There was a problem. Let's see what went wrong.<br>&nbsp; &nbsp; &nbsp; &nbsp; alert('Error: ' + result.insertResults[0]);<br>&nbsp; &nbsp; &nbsp; &nbsp; console.log(result.insertResults[0]);<br>&nbsp; &nbsp; }<br>}});
  1. the right way to do this, which I honestly don’t know, but I’m sure Rob does

    Moshe, unfortunately there isn’t a “right way” to do this at present, there are only “kludge” approaches, and your 2nd approach is the closest to what I would recommend:

&nbsp;QuoteWizardStep3Model.doQuery=false;<br>&nbsp;QuoteWizardStep3Model.updateData();


Basically this just tells Skuid to reload the Model but not run any queries. It’s still kludgy because it requires a trip to the server, but at least its a fast trip. 

On our roadmap is a plan to implement something like a model.empty() or model.removeAllRows() , but this is not available in the current release, so that above approach is the best for “clearing a Model”.