Examples
SuiteScript Email Notification
Sending Email Notifications
SuiteScript email notification uses N/email for alerts.
Introduction to SuiteScript Email Notifications
SuiteScript provides a powerful way to send email notifications programmatically using the N/email
module. This is particularly useful for alerting users about important events or changes within NetSuite. In this guide, we will explore how to use the N/email
module to send emails with SuiteScript.
Setting Up Your Script
To use the N/email
module, you need to ensure that your SuiteScript file is properly configured. The script must have access to the N/email
module. Below is an example of how to set up your script header to include the necessary module:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/email', 'N/record', 'N/log'],
function(email, record, log) {
function afterSubmit(context) {
// Script logic will go here
}
return {
afterSubmit: afterSubmit
};
});
Sending an Email Notification
Once the N/email
module is included, you can use its send
method to dispatch emails. The following example demonstrates how to send an email notification after a record is updated:
function afterSubmit(context) {
if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) {
return;
}
var newRecord = context.newRecord;
var recordId = newRecord.id;
var recordType = newRecord.type;
email.send({
author: -5, // Internal ID of the author. -5 represents the default system sender
recipients: 'example@domain.com',
subject: 'Record Update Notification',
body: 'A record with ID ' + recordId + ' of type ' + recordType + ' has been updated.'
});
log.debug('Email Sent', 'Notification email has been sent successfully.');
}
Handling Common Errors
When using the N/email
module, you might encounter certain errors. Common issues include invalid email addresses and insufficient permissions. It is important to handle these errors gracefully to ensure the reliability of your script.
try {
email.send({
author: -5,
recipients: 'example@domain.com',
subject: 'Record Update Notification',
body: 'A record with ID ' + recordId + ' has been updated.'
});
} catch (e) {
log.error('Email Send Failure', e.message);
}
Conclusion
Using the N/email
module in SuiteScript allows for seamless integration of email notifications into your NetSuite workflows. By following the examples provided, you can customize email alerts to suit your business needs, ensuring important information is communicated efficiently.
Examples
- Previous
- Record Update
- Next
- File Upload