2

I'm following the steps in this video to create a Comments Section.

James (the author) created two models, then creates a relation between the two, then creates a table to add/edit comments/notes.

He uses the following Event in one model:

onCreate    
record.reported_by = Session.getActiveUser().getEmail();    
record.Date = new Date();

and the following Event in the second.

onCreate
record.Tech = Session.getActiveUser().getEmail();
record.Date = new Date();

Now if you have a label that is bound to the relationship, it will show the email address of the user who created the new item.

All that is working perfectly. I was just trying to figure out a way to have the label display the Full Name instead of the Email Address.

I was hoping it would be something simple like switching getEmail with something like getFullName, but no such luck.

Adam Bergeron
  • 535
  • 3
  • 11

1 Answers1

3

Afaik at this time there is no built-in method in App Maker to get current user Full Name, but you can get it by adding Directory model to your application. After you added Directory model you have multiple ways to serve user's Full Name:

1 Save it to your database when record is created

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

and then use this 'reported_full_name' in binding instead of 'reported_by'.

  • pros - better performance when you render multiple records on UI
  • cons values in your DB can eventually become obsolete

2 Use calculated model and make dynamic 'virtual relation' between your local model and Directory

  • pros - you will always have actual data
  • cons - it will work slower, it will be harder to maintain and implement

3 Query server for Full Name after datasource is loaded on UI

// widget binding
setFullName(widget, @datasource.item.reported_by)

// client script
setFullName(widget, email) {
  google.script.run
        .withSuccessHandler(function(fullName) {
           widget.text = fullName;
         })
         .getFullName(email);
}

// server script
function getFullName(email) {
  var directoryQuery = app.models.Directory.newQuery();
  directoryQuery.filters.PrimaryEmail._equals = email;
  var person = directoryQuery.run()[0];

  return person.FullName;
}
  • pros - you will always have actual data
  • cons - labels on UI will blink, worse performance due to big number requests to server
Pavel Shkleinik
  • 6,298
  • 2
  • 24
  • 36
  • 1
    Maybe you also want your app to look better and more social, then you can store ThumbnailPhotoUrl in database as well and bound it to Image widget. – Pavel Shkleinik Jan 28 '17 at 00:48