Send email action on workflow keeps sending the em...
# ask-stanley-ai
a
Send email action on workflow keeps sending the email everytime the record it saved. I want it to just send once. What's the easiest way to achieve this?
c
AI generated answer (using internal Slack threads):
To prevent the workflow from sending an email every time the record is saved, you can add a condition to the 'Send Email' action. This condition can be a checkbox field on the record that gets checked after the email is sent. Here's a step-by-step guide on how to do this: 1. Create a custom checkbox field on the record type where the workflow is running. Let's call this field 'Email Sent'. 2. In your workflow, add a 'Set Field Value' action that sets the 'Email Sent' field to 'T' (true). This action should be placed after the 'Send Email' action. 3. Add a condition to the 'Send Email' action that checks if the 'Email Sent' field is 'F' (false). This means the email will only be sent if the 'Email Sent' checkbox is not checked. This way, the email will only be sent the first time the record is saved (when the 'Email Sent' checkbox is still unchecked). After the email is sent, the 'Email Sent' checkbox will be checked, preventing the email from being sent again the next time the record is saved. Here's how you can set this up in SuiteScript:
Copy code
javascript
var rec = record.load({
    type: record.Type.SALES_ORDER,
    id: '123'
});

if (rec.getValue('custbody_email_sent') == false) {
    // Send email
    email.send({
        author: 123,
        recipients: '<mailto:abc@xyz.com|abc@xyz.com>',
        subject: 'Test Email',
        body: 'Hello, this is a test email.'
    });

    // Set 'Email Sent' checkbox to true
    rec.setValue('custbody_email_sent', true);
    rec.save();
}
Please replace 'custbody_email_sent' with the actual ID of your custom 'Email Sent' field, and replace '123' and 'abc@xyz.com' with the actual author ID and recipient email. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.837714791
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.829932868
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.829864442
Please provide your feedback on this answer.