首页 文章

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

提问于
浏览
1

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

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”等于电子邮件地址(或者最好是用户的实际名称) .

例如:“ John Doe 为...创建了新的系统订单”

我错过了什么?

谢谢!

注意:我已经有一个目录模型设置,以显示用户's names in a comments section for a different Model. That Model is running the following (I' m,假设我可以将其添加到我的SystemOrders模型?)

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

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

1 回答

  • 4

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

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

    如果要从目录中使用用户全名,则需要预先加载它,例如在app启动脚本中:

    // 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);  
    });
    

相关问题