尝试在通知电子邮件中包含电子邮件地址(或用户名)

Trying To Include Email Address (or User's Name) In Notification Email

我有一个允许用户创建条目的页面片段。当他们单击发送按钮时,它会运行以下命令:

newSOEmailMessage(widget);
widget.datasource.createItem();
app.closeDialog();

这会激活一个客户端脚本,该脚本会向用户发送一封电子邮件,其中包含来自小部件字段的值:

function newSOEmailMessage(sendButton) {
  var pageWidgets = sendButton.root.descendants;
  var currentuser = Session.getActiveUser().getEmail();
  var htmlbody = currentuser + 'has created new system order for: <h1><span style="color:#2196F3">' + pageWidgets.ShowName.value + ' - ' + pageWidgets.UsersPosition.value + '</h1>' +
      '<p>R2 Order #: <b>' + pageWidgets.R2OrderNumber.value + '</b>' +
      '<p>Delivery Date: <b>' + pageWidgets.DeliveryDate.value.toDateString() + '</b>' +
      '<p>Start of Billing: <b>' + pageWidgets.SOB.value.toDateString() + '</b>' +
      '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' + 
      '<p>&nbsp;</p>' +
      '<p>Company: <b>' + pageWidgets.Company.value + '</b>' +          
      '<p>&nbsp;</p>' +
      '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>';

  google.script.run
    .withSuccessHandler(function() {
     })
    .withFailureHandler(function(err) {
      console.error(JSON.stringify(err));
    })
    .sendEmailCreate(
      'user@email.com',
      'New order for: ' + pageWidgets.ShowName.value + ' - ' + pageWidgets.UsersPosition.value,
      htmlbody);
}

除了 "currentuser" 选项(在 var htmlbody = 之后)之外,所有这些都工作正常。使用上面的代码我得到以下错误:

Session is not defined
at newSOEmailMessage (Notifications_ClientScripts:7:45)
at SystemOrders_Add.SubmitButton.onClick:1:1

我希望 "currentuser" 等于电子邮件地址(或者最好是用户的真实姓名)。

ex: "John Doe 为...创建了一个新的系统订单"

我错过了什么?

谢谢!

注意:我已经有一个目录模型设置,可以在不同模型的评论部分显示用户名。该模型是 运行 以下(我假设我可以将其添加到我的 SystemOrders 模型中?)

// onCreate
var email = Session.getActiveUser().getEmail();

var directoryQuery = app.models.Directory.newQuery();
directoryQuery.filters.PrimaryEmail._equals = email;
var reporter = directoryQuery.run()[0];

看起来您正在混合使用服务器端和客户端 API

// It is server side API
var email = Session.getActiveUser().getEmail();

// It is client side API
var email = app.user.email;

如果要使用目录中的用户全名,则需要提前加载它,例如在应用程序启动脚本中:

// App startup script
// CurrentUser - assuming that it is Directory model's datasource
// configured to load record for current user.
loader.suspendLoad();
app.datasources.CurrentUser.load({
  success: function() {
    loader.resumeLoad();
  },
  failure: function(error) {
   // TODO: Handle error
  }
});

因此,您稍后可以在代码中引用此数据源项:

var fullName = app.datasources.CurrentUser.item.FullName;

此外,我建议仅在实际创建记录时才发送电子邮件:

// Sends async request to server to create record
widget.datasource.createItem(function() {
   // Record was successfully created
   newSOEmailMessage(widget);  
});