Adam Bergeron
Adam Bergeron

Reputation: 525

Error With "null" Field In Email Notification Script

I apologize. I am sure this is a rookie question, with a simple answer.

So I am using the following to send an email notification when a user creates an entry. Everything works fine, except two behaviors related to "null" (blank) entries.

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.ShowName.value  + '</h1>' +
      '<p>Shoot Date: <b>' + pageWidgets.ProjectDate.value.toDateString() + '</b>' +
      '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +      
      '<p>Post AP: <b>' + pageWidgets.PostAP.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.ShowName.value,
      htmlbody);
  return sendButton === "" || sendButton === null || sendButton === undefined;
}

Issue 1: Whenever a user leaves a field blank the email puts "null" in the field, which makes sense, but people keep asking me "Who is null?" Ugh.

So if there is a way to output a blank space (ex:"") instead of "null" that would be great.

Issue 2: Whenever a user leaves the ProjectDate (which is a date field) blank I get the error: Cannot read property 'toDateString' of null.

This makes sense, but obviously this causes this script to not complete. I was hoping something like this would fix both issues, but it didn't:

function newSalesEmailMessage(sendButton) {
  var pageWidgets = sendButton.root.descendants;
  if (pageWidgets === null) {
    pageWidgets = "";
  }
  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.ShowName.value  + '</h1>' +
      '<p>Shoot Date: <b>' + pageWidgets.ProjectDate.value.toDateString() + '</b>' +
      '<p>Contact: <b>' + pageWidgets.Contact.value + '</b>' +      
      '<p>Post AP: <b>' + pageWidgets.PostAP.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.ShowName.value,
      htmlbody);
}

Thank you for your help.

Upvotes: 0

Views: 264

Answers (1)

Pavel Shkleinik
Pavel Shkleinik

Reputation: 6347

There are multiple ways to handle such situations, the easiest one will be introducing some helper functions:

// This function will return empty string in case widget's value is null
// You can return any default value or even pass it as function's parameter
function getSafeString(widget) {
  return widget.value === null ?
         '' :
         widget.value;
}

function getSafeDate(widget) {
  return widget.value === null ?
         '' :
         widget.value.toDateString();
}

function newSalesEmailMessage(sendButton) {
...
'...some HTML...' + getSafeString(widgets.SomeWidget)  + '...some HTML...' +
'...some HTML...' + getSafeDate(widgets.DateWidget)  + '...some HTML...'
...
}

By the way, I would also suggest using datasource.item (or datasource.modes.create.item, depending on your bindings), to get user inputs.

Upvotes: 2

Related Questions