Email Automation

I am trying to automate my email processes. You gave me the following code to send out an email with a report attached. I have commented out the pertinent instructions, but it does work for 1 report and 1 email if I un-comment the proper lines.

function EmailSelectedReports(five, context, result)  {

    const _five = five;
    // const sendTo = five.field.EmailReportsToKey;
    five.log('EmailSelectedReports');
    // five.log(`EmailSelectedReports: sendToType = "${five.field.EmailReportsToKey}"`);
    
    // const exportResult = ExportReports(five, sendTo);
    // five.showMessage(JSON.stringify(exportResult));
    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    // Execute the report action
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /*
    const reportResult = five.executeAction('Roster', {UserKey: five.variable.UserKey});
    if (reportResult.isOk() === false)   {
        return five.createError(reportResult);
    }
    */
    ////////////////////////////////////////////////////////////////////////////////////////////////
    // The reportResult contains a parameter called report, which is the report result in the format
    // of a pdf (encoded as data url application/pdf mimetype)
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /*
    const mailMergeContext = {SMTPToEmail: five.field.EmailAddress, SMTPToName: "Staff", SMTPAttachments: [reportResult.report]};
    const mailResult = five.executeAction('ReportEmail', mailMergeContext);
    if (mailResult.isOk() === false) {
        return five.createError(mailResult);
    }
    */
    return five.success('Report has been sent');
}

Now I’m developing a new form to send emails both on-demand and automated. Currently I’m working on the on-demand portion.

I have a table called SelectedReports. My intent is to write a server function to collect reportResult objects in a collection or dictionary object, one object per selected report. I will later use this dictionary to power the mail merge.

ChatGPT thinks the reportResult object is too complex to send back from the server in a JSON object or dictionary, because it has both properties and methods. So the first question is: Is that true, or can I repeatedly, in a server function, call the executeAction for each selected report, then store them in a JSON object to return to the client?

The reason I want to call this server function from the client is that there will be several decisions to make after returning the object, before I actually create the emails and add the appropriate reports to them.

For the on-demand emails, there may or may not be reports to attach. For the 2 types of automated emails, there will be reports to attach. So my plan is to first call a server function to get the exported report(s) information, then either use it or not for sending the emails.

Thanks…

Hi Ron,

I don’t suggest generating each report and sending its data from the back (server) to the front(client).
(especially because the execution of the email with an attachment happens in the back end ).

If you bring the report to the front, then you need to send it back again to be sent via email (this will consume more processing time and increase the complexity of your code)

What I suggest is:

  • Call the back-end (Server function), pass all necessary parameters, execute all essential extra logic, generate all reports, and send the emails.

—————————————-
Answering your question:
You can executeAction repeatedly, store each result in a different variable or create an array of objects, and return to the front. (However, I do not recomment it as explained).

Regards
Elton S

Thanks for the quick answer Elton. I appreciate it.

The question was prompted by my proposed emailing architecture. As mentioned, I have GENERIC, BOARD, and LEADERS for types of emails to send.

GENERIC is simple: choose member emails, choose report(s) to attach, type in subject and body, and send.

BOARD and LEADER are different. For each of these, there is logic controlling who gets which reports. So depending on which reports are chosen in the initial phase, these 2 types need to have logic pulling up which members will get the reports. I thought that making these steps distinct (as far as collecting the reportResult objects), that would keep the back-end logic and code simpler.

If you say these concerns are outweighed by overhead of having 2 different server calls, then I understand that. More logic on the back-end function then…

thanks again…

Thanks Elton, you have been very informative. I suppose this topic is now closed.

Please see new topic “Writing Long Text to Table”, as this is a challenge I’m having.

Thanks again, so much