Description
Send an email to the customer when an item fulfillment status is changed to “shipped”. There should be 2 email templates: the one currently used to send automated emails; another one need to be created with the Subject as “Your order is now ready for pick-up!” and message body as “Pick-up hours are Monday through Friday 9:00 am – 4:00 pm at our Missoula, MT warehouse located at 8404 El Way Drive.”
If the shipping method is “MT Pick Up” send an email to the customer with the newly created email template, else send an email using the current email template.
The sender of the email would be the person who changes the status to “shipped” and the recipient would be the customer.
Solution
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*******************************************************************************
* CLIENTNAME:Cleerline Technology Group
* CTG-44
* CTG Project
*************************************************************************
* Date : 06-03-2022
*
* Author: Jobin & Jismi IT Services LLP
* Script Description :
* The script is used to send Item fulfillment emails automatically
* Date created :06-03-2022
* Created by: Aranya T R, Jobin & Jismi IT Services LLP
* REVISION HISTORY
* Revision 1.0
*
******************************************************************************/
define(['N/email', 'N/record', 'N/runtime', 'N/search', 'N/render'], (email, record, runtime, search, render) =>
{
const beforeSubmit = (scriptContext) =>
{
try
{
if( scriptContext.type === 'edit' )
{
let IFrec = scriptContext.newRecord;
let shipStatus = IFrec.getText({ fieldId : 'shipstatus' });
log.debug("Shipping status @Load", shipStatus);
if (shipStatus === "Shipped")
IFrec.setValue ({ fieldId : 'custbody_jj_already_mailed',
value : true });
}
}
catch(e)
{
log.debug( "Error at Loading", e.message);
}
}
const afterSubmit = (scriptContext) =>
{
if (scriptContext.type === 'create' || 'edit')
{
try
{
//Accessing the new record details
let newIFRec = scriptContext.newRecord;
let fulfillID = newIFRec.id;
log.debug("Fullfillment ID", fulfillID);
let newIFStatus = newIFRec.getText({fieldId: 'shipstatus'});
log.debug("New IF status", newIFStatus);
let newShipMethod = newIFRec.getText({fieldId: 'shipmethod'});
log.debug("New IF method", newShipMethod);
//Getting Already Emailed value
let alreadyEmailed = newIFRec.getValue({fieldId: 'custbody_jj_already_mailed'});
log.debug("Already Emailed?", alreadyEmailed);
//Getting ID of Current User
let currentUser = runtime.getCurrentUser().id;
log.debug("Current User", currentUser);
//Getting Customer ID
let custID = newIFRec.getValue({fieldId: 'entity'})
log.debug("Customer", custID);
//Lookup the customer to find email ID
let custEmail = search.lookupFields({
type: "customer",
id: custID,
columns: ["email"]
}).email;
log.debug("Customer Mail", custEmail);
//When status is shipped and shipmethod is MT Pickup
if (newIFStatus === "Shipped" && newShipMethod === "MT Pick Up" && alreadyEmailed === false) {
try{//Loading the email template
let newEmailTempl = render.mergeEmail({
templateId: 45,
transactionId: parseInt(fulfillID),
entity: { type: 'customer', id: parseInt(custID) }
});
let emailSubject = newEmailTempl.subject;
log.debug("subject", emailSubject);
let emailBody = newEmailTempl.body;
//Sending Email to customer by using custom template
email.send({
author: currentUser,
recipients: custEmail,
subject: emailSubject,
body: emailBody,
relatedRecords: { transactionId: fulfillID }
});
}
catch(e)
{
log.debug( "Error at email sent", e.message);
}
}
//When status is shipped and shipmethod is NV Pickup
else if (newIFStatus === "Shipped" && newShipMethod === "NV Pick Up" && alreadyEmailed === false) {
//Loading the email template
let newEmailTempl = render.mergeEmail({
templateId: 52,
transactionId: parseInt(fulfillID),
entity: { type: 'customer', id: parseInt(custID) }
});
let emailSubject = newEmailTempl.subject;
let emailBody = newEmailTempl.body;
//Sending Email to customer by using custom template2
email.send({
author: currentUser,
recipients: custEmail,
subject: emailSubject,
body: emailBody,
relatedRecords: { transactionId: fulfillID }
});
}
//When shipmethod is not MT Pickup/NT pickup
else if (alreadyEmailed === false && newIFStatus === "Shipped" && newShipMethod !== ("MT Pick Up" && "NV Pick Up"))
{
let stndEmailTempl = render.mergeEmail
({
templateId: 48,
transactionId: parseInt(fulfillID),
entity: { type: 'customer', id: parseInt(custID) }
});
let emailSubject2 = stndEmailTempl.subject;
let emailBody2 = stndEmailTempl.body;
//Sending email using standard template
email.send({
author: currentUser,
recipients: custEmail,
subject: emailSubject2,
body: emailBody2,
relatedRecords: { transactionId: fulfillID }
});
}
}
catch (e)
{
log.debug( "Error", e.message );
}
}
}
return { beforeSubmit, afterSubmit }
});