1
votes

I want to send an email notification whenever the user clicks on a button. The button will call sendEmail(widget) function and invoke a client script as follow:

function sendEmail(widget){

  var item = widget.datasource.item;

  google.script.run
    .withFailureHandler(function(error) {
      console.text = error.message;
    })
   .withSuccessHandler(function(result) {
      console.text = 'succeed';
   })
 .sendEmails(item);
}

then it will pass the datasource on item and call sendEmails(item) function from a server script as follows:

function sendEmails(item){  

  var to = item.OwnerEmail;

  var subject = 'Please Review';

  var body = 'hello<br/>my name is Muhammad Alif bin Azali';

  MailApp.sendEmail({
      to: to,
      subject: subject,
      htmlBody: body,
      noReply: true
  });
}

but when I click the button I got following error instead. Any help?

enter image description here

2
Chances are you are not getting proper value in item object. Try adding Logger.log(item.OwnerEmail); after var to = item.OwnerEmail; line - Darpan Sanghavi
D Wed May 23 14:07:52 GMT+800 2018 [email protected] E Wed May 23 14:07:52 GMT+800 2018 Failed due to illegal value in property: a - alep
item.OwnerEmail hold the correct value - alep
Could please you remove this one in case it is the same issue - stackoverflow.com/questions/50462355? - Pavel Shkleinik

2 Answers

1
votes

Unfortunately, you cannot pass whatever you want as a parameter to your server function. Communication to the server has some limitations:

...most types are legal, but not Date, Function, or DOM element...

...objects that create circular references will also fail...

App Maker's records definitely violate those restrictions.

There are different strategies to handle this limitation, one of them is passing record's key as function's parameter.

// Client script
function sendEmail(widget) {
  var item = widget.datasource.item;

  google.script.run
  ...
 .sendEmails(item._key);
}

// Server script
function sendEmails(itemKey) {
  var item = app.models.MyModel.getRecord(itemKey);
  ...
}
1
votes

Looks like your Mail Body needs to be converted in HTML Body,

Here's the sample

function sendEmails(item){  

  var to = item.OwnerEmail;

  var subject = 'Please Review';

  var body = 'hello<br/>my name is Muhammad Alif bin Azali'; 
  var template = HtmlService.createTemplate(body);
  var htmlBody = template.evaluate().getContent();


  MailApp.sendEmail({
      to: to,
      subject: subject,
      htmlBody: htmlBody ,
      noReply: true
  });
}