Adam Bergeron
Adam Bergeron

Reputation: 525

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

I have a Page Fragment that allows users to create an entry. When they click the send button it runs the following:

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

This activates a Client Script that sends an email to the user which includes the values from the widget fields:

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(
      '[email protected]',
      'New order for: ' + pageWidgets.ShowName.value + ' - ' + pageWidgets.UsersPosition.value,
      htmlbody);
}

All of this works fine except the "currentuser" option (after var htmlbody =). With the code above I get the following error:

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

I would like "currentuser" to equal the email address (or preferably the user's actual name).

ex: "John Doe has created a new system order for..."

What am I missing?

Thank you!

Note: I already have a Directory Model setup to show user's names in a comments section for a different Model. That Model is running the following (I'm assuming I could add that to my SystemOrders model?)

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

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

Upvotes: 1

Views: 723

Answers (1)

Pavel Shkleinik
Pavel Shkleinik

Reputation: 6347

Looks like you are mixing server and client side APIs

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

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

If you want to utilize user Full Name from the directory, then you need to load it advance, for instance in 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.CurrentUser.load({
  success: function() {
    loader.resumeLoad();
  },
  failure: function(error) {
   // TODO: Handle error
  }
});

So, you can refer to this datasource item later in your code:

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

Also, I would recommend send emails only when record is actually created:

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

Upvotes: 4

Related Questions