In this user event script designed to dynamically generate PDFs by rendering data from related records.
The script utilizes modules like ‘N/record,’ ‘N/render,’ ‘N/file,’ ‘N/format,’ and ‘N/search’ for interacting with NetSuite records, rendering templates, and conducting searches.
Rendering Data from Other Records: The core functionality lies in the onRequest function, triggered by a button click. The script extracts item fulfillment details, retrieves associated sales order information, and intelligently handles invoice data.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/ui/serverWidget', 'N/search'],
/**
* @param{record} record
* @param{serverWidget} serverWidget
*/
(record, serverWidget, search) => {
/**
* 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 {
if (scriptContext.type == scriptContext.UserEventType.VIEW) {
// Create a button and add it to the form
let form = scriptContext.form;
let newRecId = scriptContext.newRecord.id;
log.debug("record id", newRecId);
var newRec = record.load({
type: record.Type.ITEM_FULFILLMENT,
id: newRecId,
isDynamic: true,
});
let sub = newRec.getValue({
fieldId: 'subsidiary',
});
if (sub == '7' || sub == '3') {
form.addButton({
id: "custpage_create_packing_slip",
label: "Create Packing Slip",
functionName: 'window.open("/app/site/hosting/scriptlet.nl?script=2002&deploy=1&itemFulfillmentId=' + newRecId + '")'
});
}
}
}
catch (e) {
log.error("error@beforeLoad", e);
}
}
return { beforeLoad }
});