1
votes

Below is the code for the custom button. The objective is to store different gender and rating for different button:

var gender='Ladies';
var rating='Good';
setRating(gender, rating);

The method implementation for setRating(gender, rating) is wrote on the client script as follow. The objective is to tell browser javascript to activate sendRating(gender, rating) function.

function setRating(gender, rating){
  google.script.run
   .withFailureHandler(function(error) {
      // An error occurred, so display an error message.
      status.text = error.message;
    })
  .withSuccessHandler(function(result) {
     // Report that the email was sent.
     status.text = 'Thank you for the feedback';
   })
 .sendRating(gender, rating);
}

Below is the sendRating(gender, rating) implementation wrote on the server script. The objective is to tell AppMaker javascript to activate .saveRecords API to save the gender and rating records to the datasource called ToiletRating. The datasource contain field such as 'Gender' and 'Rating'.

function sendRating(gender, rating){

  var db = app.models.ToiletaRating.newRecord();
  person.Gender = gender;
  person.Rating = rating;
  app.saveRecord([db]);

}

Can you help me why I get this error saying that newRecord method is undefined while I have declare it in the Server script line no. 3 .

E
Wed Feb 28 09:47:42 GMT+800 2018
TypeError: Cannot call method "newRecord" of undefined. at sendRating (NewScript:3)
1
It seems that you are not referencing your model name correctly. Are you sure the model name is ToiletaRating ? - Morfinismo

1 Answers

0
votes

It seems that you have a typo in your script (Toileta vs Toilet):

// your version with typo
var db = app.models.ToiletaRating.newRecord();

// version without typo
var db = app.models.ToiletRating.newRecord();

But your script will fail even if you fix the typo. You need to use the variable you defined to make things work:

function sendRating(gender, rating) {
  // define variable
  var newRecord = app.models.ToiletRating.newRecord();

  // use variable
  newRecord.Gender = gender;
  newRecord.Rating = rating;

  // Note that in original script you have one more typo: 
  // app.saveRecord vs app.saveRecords
  app.saveRecords([newRecord]);
}

Also keep in mind, that you can improve this script further:

  • Associate ratings with users to prevent adding multiple ratings from the same people
  • Add permission checks to prevent users adding ratings on behalf of other users
  • ...

Take a look at Vendor Ratings and Q&A Forum templates, they have very similar functionality