Reputation: 525
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
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'.
2 Use calculated model and make dynamic 'virtual relation' between your local model and Directory
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;
}
Upvotes: 3