Reputation: 525
I want a notification email to go out when a user creates an entry that contains information from that entry (or widget).
Ex:
subject: Project Name
body: "User1 has created a new project with the Status of In Progress".
I tried to look at the Project Tracker App for reference, but I'm still lost.
I assumed I would need to create a Client Script, do something like the function below and add that function to the onClick Event for "Submit Button" in the Page Fragment for creating new entries, but it isn't working.
Submit Button onClick Event:
widget.datasource.createItem();
sendEmailCreated_(to, subject, body);
app.closeDialog();
Client Script:
function sendEmailCreated_(to, subject, body) {
var widgets = widget.root.descendants;
try {
MailApp.sendEmail({
to: '[email protected]',
subject: widgets.ProjectName.value,
body: 'User1 has created a new project with the Status of' + widgets.ProjectStatus.value,
noReply: true
});
} catch (e) {
// Suppressing errors in email sending because email notifications
// are not critical for the functioning of the app.
console.error(JSON.stringify(e));
}
}
When the function tries to run it says "to" is undefined and I'm sure I'm not using the correct option for things like "widgets.ProjectName.value".
Any help would be greatly appreciated.
Thank you.
Upvotes: 1
Views: 397
Reputation: 144
In addition to not using server-side script, where MailApp can actually be accessed (as Pavel pointed out in his answer), I'm going to point out why you're getting that error.
You're using
sendEmailCreated_(to, subject, body);
from OnClick event, without ever defining to, subject or body. Instead, when trying to pass something from a widget to client script, you should use something like:
doSomething(widget);
(because the you can check from the widget that onClick allows direct access to the current widget)
And the function would be something like
function doSomething(widget) {
var variable1 = widget.value;
doSomethingElse(variable1);
}
So you need to make sure that you actually have defined the parameters you're sending to a function.
Your email example would not have those specific errors (but different ones explained by Pavel partially) if you used something along the lines of
var to = "[email protected]";
var subject = "Example subject";
var body = "Example text body";
sendEmailCreated_(to, subject, body);
Upvotes: 1
Reputation: 6347
There is no client side API to send emails. You need to move your 'sendEmailCreated_' function to Server Script and call it in onCreate model event (what it is preferable from the security perspective):
// onCreate model event receives about-to-create record
// Undersocre in the end of the function name means that
// function is private (cannot be called from the client)
sendEmailCreated_(record.to, record.subject, record.body);
...or using google.script.run in create callback(what is less secure, since you expose Mail API to the end user):
widget.datasource.createItem({
success: function(record) {
google.script.run
.withSuccessHandler(function() {
// TODO: Handle success
})
.withFailureHandler(function(error) {
// TODO: Handle failure
})
.sendEmailCreated(record.to, record.subject, record.body);
// sendEmailCreated - is a server side function.
// It is public (can be called from the client), since
// it doesn't end with underscore
},
failure: function(error) {
// TODO: Handle failure
}
});
If you don't care about security and error handling, then the client side of the code can be simplified to this:
widget.datasource.createItem(function(record) {
google.script.run.sendEmailCreated(record.to, record.subject, record.body);
});
Usefull links on the topic:
Upvotes: 2