How to stop emails manually?

Home Forums Modern DFFS How to stop emails manually?

Viewing 1 reply thread
  • Author
    Posts
    • #38343
      Amal Vellappillil
      Participant

        Hi Alex,

        I am using the exposed dffs_emailActions and dffs_buildEmailProps to dynamically retrieve email content to schedule some emails. I am running into an issue where the emails still get sent on save (it also gets scheduled). I tried the following to get around this.

        On save: schedule email and remove the email from dffs_emailActions array. I am doing it with the following code.

        async function sendScheduleNextDueDateEmail() {
          prepareEmailTemplates(); // this function has logic to trigger a rule that contains the "Prepare Email" action triggered from custom javascript 
        
          if (_ltpSPO.isEmptyArray(dffs_emailActions)) {
            return false;
          }
        
          // Convert forEach to a Promise.all with map to handle all emails
          const emailPromises = dffs_emailActions.map(async email => {
            if (email.id === "8289063629904061") {
              const props = await dffs_buildEmailProps(email);
              await scheduleNextDueDateEmail(props);
              
              const idx = dffs_emailActions.findIndex(item => item["id"] === "8289063629904061");
              if (idx > -1) {
                dffs_emailActions.splice(idx, 1);
              }
            }
          });
        
          // Wait for all email operations to complete
          await Promise.all(emailPromises);
        }

        I tried calling the above method from a rule with trigger of “Before Save”. It sometimes schedules the email, but will aways sends the emails right away as well. Seems like the saving is not waiting for the method to complete. I also tried removing that rule and calling the above function from dffs_PreSaveAction method like this.

        async function dffs_PreSaveAction(exit, autoSave) {
          if (exit) {
            // Wait for the email sending to complete before returning
            await sendScheduleNextDueDateEmail();
            return true;
          }
        }

        Email got scheduled, but it also got sent right away.

        So how can I properly stop those emails from being processed by “Save” action?

      • #38344
        Alexander Bautz
        Keymaster

          You cannot run async code before save – the built in save method will not wait for it.

          I suggest you try triggering the email creation by adding a yes / no field “Schedule emails” or something like that and when you check this field, run your code and then clear all emails in a “Before save of the form” rule.

          Alexander

      Viewing 1 reply thread
      • You must be logged in to reply to this topic.