首页 文章

试图将当前用户名放入通知电子邮件中

提问于
浏览
1

我目前有一个页面片段,其中包含以下代码,用于为数据源创建条目,并发送通知所有人的电子邮件(下面的代码) .

Button:

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

Client Script Email notification code:

/**
 * Calls a server method to send an email.
 * @param {Widget} sendButton - widget that triggered the action.
 */
function newSalesEmailMessage(sendButton) {
  var pageWidgets = sendButton.root.descendants;
  var fullName = app.datasources.Directory.item.FullName;
  var htmlbody = '<b><font size="3">' + fullName + '</font></b>' + ' has created a new sales entry for: ' +  
      '<h1><span style="color:#2196F3">' +pageWidgets.ProjectName.value  + '</h1>' +
      '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +      
      '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' +
      '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>';


  google.script.run
    .withSuccessHandler(function() {
     })
    .withFailureHandler(function(err) {
      console.error(JSON.stringify(err));
    })
    .sendEmailCreate(
      'test@email.com',
      'New Sales Entry for: ' + pageWidgets.ProjectName.value,
      htmlbody);
}

onCreate code for the Model:

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

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

record.reported_by = email;
record.reported_full_name = reporter.FullName;
record.Date = new Date();

一切都有效,除了fullName选项 . 即使其他用户创建了一个条目(也许是因为我是管理员?),它一直在拉我的名字 . 我有一个目录模型设置,这似乎适用于我显示用户评论的全名 .

我想要fullName =当前创建条目的人的姓名 .

谢谢您的帮助!

App Startup Script:

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

2 回答

  • 2

    您需要过滤目录模型数据源 . 如果您计划将其用于不同目的,那么我建议为当前用户创建专用数据源 . 您可以在服务器(首选)或客户端过滤它:

    Filter on server, load on client:

    // Directory model's Server Script for Current User datasource
    var query = app.models.Directory.newQuery();
    
    query.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
    
    return query.run();
    
    // ------------------------
    // Your startup script will remain almost the same:
    loader.suspendLoad();
    app.datasources.CurrentUser.load({
      success: function() {
        loader.resumeLoad();
      },
      failure: function(error) {
       // TODO: Handle error
      }
    });
    

    Client-only:

    var currentUserDs = app.datasources.CurrentUser;
    
    currentUserDs.query.filters.PrimaryEmail._equals = app.user.email;
    
    loader.suspendLoad();
    currentUserDs.load({
      success: function() {
        loader.resumeLoad();
      },
      failure: function(error) {
       // TODO: Handle error
      }
    });
    
  • 2

    谢谢帕维尔 . 一切都有效,但我花了一些时间才明白我需要做什么 . 对于那些想要尝试复制我在这里所做的事情的人来说,这些步骤 .

    首先,我必须创建一个Directory Model .

    然后在应用程序本身的应用程序设置部分下(单击齿轮)我将以下代码放在 App Startup Script - Client Script 部分下:

    loader.suspendLoad();
    app.datasources.CurrentUser.load({
      success: function() {
        loader.resumeLoad();
      },
      failure: function(error) {
       // TODO: Handle error
      }
    });
    

    接下来,我进入了Directory模型的Datasources部分,并添加了一个名为CurrentUser的数据源 .

    Query - Server Script 部分,我提出:

    var query = app.models.Directory.newQuery();
    
    query.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();
    
    return query.run();
    

    这将过滤数据源,以便其中唯一的条目是当前用户 . 然后我调整了电子邮件客户端脚本中的“var fullName”以指向新的数据源:

    /**
     * Calls a server method to send an email.
     * @param {Widget} sendButton - widget that triggered the action.
     */
    function newSalesEmailMessage(sendButton) {
      var pageWidgets = sendButton.root.descendants;
      var fullName = app.datasources.CurrentUser.item.FullName;
      var htmlbody = '<b><font size="3">' + fullName + '</font></b>' + ' has created a new sales entry for: ' +  
          '<h1><span style="color:#2196F3">' +pageWidgets.ProjectName.value  + '</h1>' +
          '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +      
          '<p>Sales Person: <b>' + pageWidgets.SalesPerson.value + '</b>' +
          '<p>Notes: <b>' + pageWidgets.Notes.value + '</b>';
    
    
      google.script.run
        .withSuccessHandler(function() {
         })
        .withFailureHandler(function(err) {
          console.error(JSON.stringify(err));
        })
        .sendEmailCreate(
          'test@email.com',
          'New Sales Entry for: ' + pageWidgets.ProjectName.value,
          htmlbody);
    }
    

相关问题