How to automatically Populate NetSuite’s Email Composer with Additional Recipients Sublist?

We can add a User Event – beforeLoad on the Message record to auto populate the custom fields from the custom record to the Email popup.

Use the below code to set the value.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/********************************************************************************************************************
* 
* Magswitch Technology-USA-NS
*
* MAGT-1224 : Multiple Email Population in Email Message Record.
*
********************************************************************************************************************
* 
* Author: Jobin & Jismi
*
* Date Created: 12-September-2025
*
* COPYRIGHT © 2025 Jobin & Jismi. All rights reserved. 
* This script is a proprietary product of Jobin & Jismi and is protected by copyright law and international treaties.
* Unauthorized reproduction or distribution of this script, or any portion of it, may result in severe civil and criminal penalties, 
* and will be prosecuted to the maximum extent possible under the law.
*
* Description: This script is used to set the email address in the email message record of the customer.
*
* REVISION HISTORY
*
* @version 1.0 MAGT-1224: 12-September-2025 : Initial build by JJ0337.
*
******************************************************************************************************************/
define(['N/search'],
    /**
     * @param {*} search 
     * @returns 
     */
    (search) => {
        "use strict";
        const EMAIL_MSG_ENTITY = ['custjob'];
        /**
         * @description : This function is used to set the email address in the email message record of the customer.
         * @param {*} messageRecord - The email message record.
         * @param {*} customerId - The customer Id. 
         * @returns 
         */
        function setEmailAddress(messageRecord, customerId) {
            try {
                if (!customerId) return;
                let emailIds = search.lookupFields({
                    type: search.Type.CUSTOMER,
                    id: customerId,
                    columns: ['email', 'custentity_jj_email1_magt1157', 'custentity_jj_email2_magt1157', 'custentity_jj_email3_magt1157']
                })
                let primaryEmail = [emailIds.email];
                let emails = [
                    emailIds.custentity_jj_email1_magt1157,
                    emailIds.custentity_jj_email2_magt1157,
                    emailIds.custentity_jj_email3_magt1157
                ];
                let existingEmails = [];
                let existingCount = messageRecord.getLineCount({ sublistId: 'otherrecipientslist' });
                for (let i = 0; i < existingCount; i++) {
                    let existingEmail = messageRecord.getSublistValue({
                        sublistId: 'otherrecipientslist',
                        fieldId: 'email',
                        line: i
                    });
                    if (existingEmail) {
                        existingEmails.push(existingEmail.toLowerCase());
                    }
                }
                let index = existingCount;
                for (let email of emails) {
                    if (email && !existingEmails.includes(email.toLowerCase()) && !primaryEmail.includes(email.toLowerCase())) {
                        messageRecord.setSublistValue({
                            sublistId: 'otherrecipientslist',
                            fieldId: 'email',
                            line: index,
                            value: email
                        });
                        messageRecord.setSublistValue({
                            sublistId: 'otherrecipientslist',
                            fieldId: 'toRecipients',
                            line: index,
                            value: true
                        });
                        existingEmails.push(email.toLowerCase());
                        index++;
                    }
                }
            } catch (error) {
                log.error('error @ setEmailAddress', error);
            }
        }


        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            try {
                let messageRecord = scriptContext.newRecord;
                let type = messageRecord.getValue('entitytype');
                if (!EMAIL_MSG_ENTITY.includes(type)) {
                    return;
                }
                let customerId = messageRecord.getValue('entity');
                setEmailAddress(messageRecord, customerId);
            } catch (error) {
                log.error('error @ beforeLoad', error);
            }
        }


        return { beforeLoad }
    });

Leave a comment

Your email address will not be published. Required fields are marked *