Adam Bergeron
Adam Bergeron

Reputation: 525

Need To Show Directory Output as Full Name Instead of Email Address

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.

Upvotes: 2

Views: 355

Answers (1)

Pavel Shkleinik
Pavel Shkleinik

Reputation: 6347

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

Upvotes: 3

Related Questions