Introduction:
Creating dynamic URLs for approval processes is crucial in automating workflows in NetSuite. This article discusses generating external and record URLs to streamline item approval processes.
const sendEmail = {
sendEmail(recordId, itemName) {
try {
const CUST_RECORD_TYPE_INVENTORY = "customrecord_grw007_invtitem";
let script = runtime.getCurrentScript();
let recipientEmail = script.getParameter({ name: 'custscript_grw_021_item_approv_recipient' });
let emailTemplateId = script.getParameter({ name: 'custscript_grw_021_approval_email' });
log.debug("recipientEmail", recipientEmail);
log.debug("emailTemplateId", emailTemplateId);
let extURL = url.resolveScript({ // getting External URL of the suitelet of Feedback
deploymentId: 'customdeploy_grw_021_sl_approve_item',
scriptId: 'customscript_grw_021_sl_approve_item',
returnExternalUrl: true
});
log.debug("extURL", extURL);
let recordURL = url.resolveRecord({
recordType: CUST_RECORD_TYPE_INVENTORY,
recordId: recordId,
isEditMode: false
});
let approvalUrl = extURL + '&recordId=' + recordId + '&action=approve' + '&itemName=' + itemName;
let rejectionUrl = extURL + '&recordId=' + recordId + '&action=reject' + '&itemName=' + itemName;
log.debug("approvalUrl", approvalUrl);
log.debug("rejectionUrl", rejectionUrl);
let newEmailTempl = render.mergeEmail({
templateId: emailTemplateId,
});
let emailBody = newEmailTempl.body;
let emailSubject = newEmailTempl.subject;
emailBody = emailBody.replaceAll('{{approvalUrl}}', approvalUrl);
emailBody = emailBody.replaceAll('{{rejectionUrl}}', rejectionUrl);
emailBody = emailBody.replaceAll('{{itemName}}', itemName);
emailBody = emailBody.replaceAll('{{itemId}}', recordId);
emailBody = emailBody.replaceAll('{{recordRedirect}}', recordURL);
emailSubject = emailSubject.replaceAll('{{itemName}}', itemName);
email.send({
author: -5,
recipients: recipientEmail,
subject: emailSubject? emailSubject: "Approval Needed for Item '" + itemName + "' wit id " + recordId,
body: emailBody,
relatedRecords: {
customRecord: {
id: recordId,
recordType: CUST_RECORD_TYPE_INVENTORY
}
}
});
// Update the record status to approved
record.submitFields({
type: CUST_RECORD_TYPE_INVENTORY,
id: recordId,
values: {
custrecord_grw007_first_approver_email: true
}
});
return { status: "SUCCESS", reason: "EMAIL_SENT", data: [] };
} catch (e) {
log.error({ title: "Error @grw_021_cm_functions @sendEmail @sendEmail", details: e });
return { status: "ERROR", reason: "ERROR_IN_SENDING_EMAIL", data: [] };
}
}
};
Detailed Explanation:
- External URL:
url.resolveScript(): Generates an external URL for the suitelet handling item approvals.deploymentIdandscriptId: Specify the deployment and script IDs, respectively.returnExternalUrl: true: Ensures the URL is accessible externally.- Record URL:
url.resolveRecord(): Generates the URL for accessing the specific inventory item record.- Parameters include
recordType,recordId, andisEditMode. - Approval and Rejection URLs:
- Concatenates
extURLwith parameters forrecordId,action, anditemNameto createapprovalUrlandrejectionUrl.