Search code examples
google-app-maker

App Maker: Sending Email, client script to server script function not working. Failed due to illegal value in property: a


I am new to AppMaker but I have developer experience.

The application is a Project Tracker Application

What I expect to happen: When creating a project the user uses a User Picker to select the users associated with that project. When the project is created I want to email the users associated with that project.

The issue: On clicking the Add button addProject(addButton) client script function is called. Inside this function sendEmailToAssignees(project, assignees) is called which should reach out to the Server script and run the notifyAboutProjectCreated(project, assignees) but that is not happening.

Things to know: With logging I never reach 'Trying to send email' so I seem to never reach my server script. Also, On client script when I comment out sendEmailToAssignees function everything runs smooth. I have looked at this documentation as a resource so I feel my implementation is okay. https://developers.google.com/appmaker/scripting/client#client_script_examples

The final error message I get is:

Failed due to illegal value in property: a at addProject (AddProject:110:24) at AddProject.Container.PanelAddProject.Form1.Spring.ButtonAdd.onClick:1:1

Am I missing something here? Any help would be greatly appreciated. Thank you!

Client Script

function sendEmailToAssignees(project, assignees) {
  google.script.run
    .withSuccessHandler(function() {
    console.log('Sending Email Success');
  }).withFailureHandler(function(err) {
   console.log('Error Sending Email: ' + JSON.stringify(err));
  })
   .notifyAboutProjectCreated(project, assignees);
}

function addProject(addButton) {
  if (!addButton.root.validate()) {
    return;
  }

  addButton.datasource.createItem(function(record) {  

    var page = app.pages.AddProject;
    var pageWidgets = page.descendants;
    var trainees = pageWidgets.AssigneesGrid.datasource.items;

    var traineesEmails = trainees.map(function(trainee) {
    return trainee.PrimaryEmail;
    });

    record.Assignee = traineesEmails.toString();
    var assignees = traineesEmails.toString();
    var project = record; 

    updateAllProjects(record);
    console.log('update all projects done');    

    sendEmailToAssignees(project, assignees);
    console.log('Send Email done');

    if (app.currentPage !== app.pages.ViewProject) {
      return;
    }              
    gotoViewProjectPageByKey(record._key, true);


  });

  gotoViewProjectPageByParams();
}

Server Script

function notifyAboutProjectCreated(project, assignees) {
  console.log('Trying to send email');
  if (!project) {
    return;
  }

  var settings = getAppSettingsRecord_()[0];

  if (!settings.EnableEmailNotifications) {
    return;
  }

  var data = {
    appUrl: settings.AppUrl,
    assignee: project.Assignee,
    owner: project.Owner,
    startDate: project.StartDate,
    endDate: project.EndDate,
    jobType: project.Type,
    jobId: project.Id
  };

  // Email Subject
  var subjectTemplate = HtmlService.createTemplate(settings.NotificationEmailSubjectJob);

  subjectTemplate.data = data;

  var subject = subjectTemplate.evaluate().getContent();

  // Email Body

   var emailTemplate =
      HtmlService.createTemplate(settings.NotificationEmailBodyJob);

  emailTemplate.data = data;

  var htmlBody = emailTemplate.evaluate().getContent();

  console.log('About to send email to:', assignees);

  sendEmail_(null, assignees, subject, htmlBody);    

}

Solution

  • The reason you are getting this error is because you are trying to pass the client "project record" to the server. If you need to access the project, then pass the record key to the server and then access the record on the server using the key.

    CLIENT:

    function sendEmailToAssignees(project, assignees) {
      var projectKey = project._key;
      google.script.run
        .withSuccessHandler(function() {
        console.log('Sending Email Success');
      }).withFailureHandler(function(err) {
       console.log('Error Sending Email: ' + JSON.stringify(err));
      })
       .notifyAboutProjectCreated(projectKey , assignees);
    }
    

    SERVER:

    function notifyAboutProjectCreated(projectKey, assignees) {
      console.log('Trying to send email');
      var project = app.models.<PROJECTSMODEL>.getRecord(projectKey);
      if (!project) {
        return;
      }
    
     //Rest of the logic
    }
    

    The project record object in the client is not the same as the project record object in the server; hence the ilegal property value error.