User Event script
The server script snippet below is written in SuiteScript 2.0. It adds a “Print Invoice” button to the Sales Order form when it’s viewed. First, a variable CS is defined to hold the path to the client script. The script then checks if the context type is “view” to ensure the button is added only when the Sales Order form is in view mode. It then attaches the client script to the form using context.form.clientScriptModulePath = CS;. Finally, the context.form.addButton method is used to add the custom button, specifying its unique identifier, label, and the client-side function to call when the button is clicked.
var CS = "clientscript_path";
if (context.type == "view") {
context.form.clientScriptModulePath = CS;
context.form.addButton({
id: 'custpage_print_invoice',
label: 'Print Invoice',
functionName: 'printInvoice'
});
}
Client script
The client script defines the functions that handle the page initialization and button click events. The pageInit function initializes the current record when the page loads. The printInvoice function is triggered when the custom button is clicked. It retrieves the current Sales Order record and its ID, then defines URLs for the invoice and packing slip PDFs. These URLs are opened in new tabs using window.open, allowing the user to view and print the documents. The script concludes by exporting the pageInit and printInvoice functions to be used by the client script.
function pageInit(scriptContext) {
}
function printInvoice() {
var soRecord = currentRecord.get();
var soRecordId = soRecord.id;
var invoiceUrl = "url_for_pdf_1";
var pickTickUrl = "url_for_pdf_2";
window.open(invoiceUrl, '_blank');
window.open(pickTickUrl, '_blank');
}
return {
pageInit: pageInit,
printInvoice: printInvoice
};