Save model using Snippet

  • 1
  • Question
  • Updated 4 years ago
  • Answered
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]);

   }

}});
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb

Posted 4 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
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();
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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.
Photo of Moshe Karmel

Moshe Karmel, Champion

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

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
Wow everyone is either really bored or really fast today :)
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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!
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
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. 
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
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]);

   }

}});
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
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
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
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
Photo of Eddie

Eddie

  • 720 Points 500 badge 2x thumb
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]);

   }

}});
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
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
var models = skuid.model.map();
var QuoteWizardStep3Model = models.QuoteWizardStep3;
// get a reference to the quote model
var Quote = models.Quote;
//get a reference to the Id condition
var quoteCondition = quote.getConditionByName('MyConditionName');
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
//set the condition
quote.setCondition(quoteCondition,r.Id);
//update the models
   skuid.model.updateData([Quote,QuoteWizardStep3Model]);
    } else {
        // There was a problem. Let's see what went wrong.
        alert('Error: ' + result.insertResults[0]);
        console.log(result.insertResults[0]);
    }
}});
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
3) 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:

 QuoteWizardStep3Model.doQuery=false;
 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".