Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Created March 2, 2019 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 23maverick23/96efa38a72056497f526cdf3ec7b1d1d to your computer and use it in GitHub Desktop.
Save 23maverick23/96efa38a72056497f526cdf3ec7b1d1d to your computer and use it in GitHub Desktop.
OA: Send email on resource request
/**
* The first time a value is selected it should generate an email
* to the original submitter to let them know the status of the
* request has been changed and indicate the new status. IF the
* value of that field changes again, then another email needs to
* be sent indicating the change from one status to another. Also,
* if the email can grab any notes that are in the notes field
* that would be great.
*
* @param {String} type Form event type.
* @return {None}
*/
function main(type) {
var copy = NSOA.context.getParameter("demo_email_address") || "ryan.demo@netsuite.com";
var status = "resource_request_status__c";
var recNew = NSOA.form.getNewRecord();
var dirty = false;
if (type == "new" && recNew[status] !== "") {
// Set dirty if new record and status is set to something
dirty = true;
} else if (type == "edit") {
var recOld = NSOA.form.getOldRecord();
if (recOld[status] !== recNew[status] && recNew[status] !== "") {
// Set dirty if value is different on edit and value is not blank/no selection
dirty = true;
}
}
if (!dirty || dirty === false) {
NSOA.meta.log("debug", "Status didn't change - not sending mail");
return;
}
var txtStatus = recNew[status];
var txtNotes = recNew.notes;
var txtRequestName = recNew.name;
var idStaffingPlan = recNew.resource_request_id;
var recStaffingPlan = NSOA.record.oaResourceRequest(idStaffingPlan);
var recSubmitter = recStaffingPlan.ownerid;
var txtStaffingPlanNumber = recStaffingPlan.number;
var idProject = recStaffingPlan.projectid;
var recProject = NSOA.record.oaProject(idProject);
var txtProjectName = recProject.name;
var txtCustomerName = recProject.customer_name;
var data = {};
data.recipient = recSubmitter;
data.copy = copy;
data.subject = "Resource request - Additional status change";
data.content = "" +
"<b>Customer: </b>" + txtCustomerName + "<br>" +
"<b>Project: </b>" + txtProjectName + "<br>" +
"<b>Staffing Plan: </b>" + txtStaffingPlanNumber + "<br>" +
"<b>Request Name: </b>" + txtRequestName + "<br>" +
"<b>Additional Status: </b>" + txtStatus + "<br>" +
"<b>Notes: </b>" + txtNotes +
"";
NSOA.meta.log("debug", "Mail content -> " + JSON.stringify(data));
var mail = sendEmail(data);
NSOA.meta.log("debug", "Mail was queued to send? -> " + mail);
}
/**
* Helper function for sending OpenAir mail.
* @param {Object} data An object containing email data.
* @return {Boolean} True if sent, false if not.
*/
function sendEmail(data) {
var msg = {
to: [data.recipient],
cc: [data.copy],
subject: data.subject,
format: "HTML",
body: data.content
};
var mail = NSOA.meta.sendMail(msg);
return mail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment