1
votes

I currently have a Page Fragment with the following code that creates an entry for a datasource and sends out an email (code below) notifying everyone.

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(
      '[email protected]',
      '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();

Everything works except for the fullName option. It keeps pulling my name even when another user creates an entry (maybe because I am an admin?). I have a Directory Model setup and that seems to work for when I am displaying the full name for a users's comments.

I would like to have fullName = the name of the person currently creating the entry.

Thank you for your help!

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
Can you add to the question code that you use to filter/load Directory datasource? I can guess, that you do not filter it at all and your account is the very first record in the results... - Pavel Shkleinik
Are you talking about the App Startup Script? If so I have added it to the question. (Thank you) - Adam Bergeron

2 Answers

2
votes

You need to filter your Directory model datasource. If you are planning to use it for different purposes, then I'll recommend to create dedicated datasource for current user. You can filter it on server (preferable) or client side:

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
votes

Thank you to Pavel. Everything worked, but it took me a few to understand exactly what I needed to do. For those who want to try and replicate what I did here were the steps.

First I had to create a Directory Model.

Then under the App Settings section for the app itself (click the gear) I put the following code under the App Startup Script - Client Script section:

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

Next I went under the Datasources section for the Directory model and added a datasource called CurrentUser.

In the Query - Server Script section I put:

var query = app.models.Directory.newQuery();

query.filters.PrimaryEmail._equals = Session.getActiveUser().getEmail();

return query.run();

This filters the datasource so that the only entry in there is the current user. Then I adjusted my "var fullName" in the email Client Script to point to the new datasource:

/**
 * 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(
      '[email protected]',
      'New Sales Entry for: ' + pageWidgets.ProjectName.value,
      htmlbody);
}