N/email Module Script Sample

The following script sample demonstrates how to use the features of the N/email module.

Send an Email with an Attachment

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

The following sample shows how to send an email with an attachment.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Note:

Some of the values in this sample are placeholders, such as the senderId and recipientEmail values. Before using this sample, replace all hard-coded values, including IDs and file paths, with valid values from your NetSuite account. If you run a script with an invalid value, the system may throw an error.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * @NApiVersion 2.1
 */

// This script sends an email with an attachment.

require(['N/email', 'N/record', 'N/file'], (email, record, file) => {
    const senderId = -515;
    const recipientEmail = 'notify@myCompany.com';
    let timeStamp = new Date().getUTCMilliseconds();

    let recipient = record.create({
        type: record.Type.CUSTOMER,
        isDynamic: true
    });
    recipient.setValue({
        fieldId: 'subsidiary',
        value: '1'
    });
    recipient.setValue({
        fieldId: 'companyname',
        value: 'Test Company' + timeStamp
    });
    recipient.setValue({
        fieldId: 'email',
        value: recipientEmail
    });

    let recipientId = recipient.save();

    let fileObj = file.load({
        id: 88
    });

    email.send({
        author: senderId,
        recipients: recipientId,
        subject: 'Test Sample Email Module',
        body: 'email body',
        attachments: [fileObj],
        relatedRecords: {
            entityId: recipientId,
            customRecord: {
                id: recordId,
                recordType: recordTypeId
            }
        }
    });
}); 

          

General Notices