We will be creating 3 Custom Print Buttons.These buttons will be appearing only on the ‘View’ mode of the Invoice,depending upon the shipping country.
One of the issues faced in this requirement:-
If we select a custom form in Invoice and then use any of these custom buttons even the form is specified in the script, we get the PDF as the format of custom form we selected in invoice. The solution for this issue is we have to set one of the custom form as ‘PREFERED’.
UserEvent Script
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/render', 'N/ui/dialog', 'N/ui/serverWidget','N/search'],
/**
* @param{record} record
* @param{render} render
* @param{dialog} dialog
* @param{serverWidget} serverWidget
*/
(record, render, dialog, 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
*/
function beforeLoad(scriptContext) {
try {
if (scriptContext.type == 'view') {
var custRec = scriptContext.form;
var currentrec = scriptContext.newRecord;
var ship_add = currentrec.getValue({
fieldId: 'shipcountry'
});
if (ship_add =="DE") {
custRec.addButton({
id: 'custpage_print_invoice',
label: 'Print Invoice',
functionName: 'printinvoice'
});
} else {
custRec.addButton({
id: 'custpage_commerical_invoice',
label: 'Print Commerical Invoice',
functionName: 'commericalinvoice'
});
custRec.addButton({
id: 'custpage_packing_list',
label: 'Print Packing List',
functionName: 'packinglist'
});
}
scriptContext.form.clientScriptFileId = '6277';
}
} catch (error) {
log.error('beforeLoad', error.message);
}
}
return {beforeLoad:beforeLoad};
});
Client Script:
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/https', 'N/url','N/search'],
/**
* @param{currentRecord} currentRecord
* @param{https} https
* @param{url} url
*/
function(currentRecord, https, url,search) {
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function pageInit(scriptContext) {
try {
console.log('in');
} catch (e) {
console.log(e.name, e.message);
}
}
function printinvoice(){
var currentRec = currentRecord.get();
var recId = currentRec.id;
var type = currentRec.type;
var fieldLookUp = search.lookupFields({
type:search.Type.INVOICE,
id:recId,
columns:['customform']
});
var customForm = fieldLookUp.customform[0].value;
log.debug('form',customForm);
if(customForm==121)
{
window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=121&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
}
else
{
window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=115&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
}
}
function commericalinvoice(){
var currentRec = currentRecord.get();
var recId = currentRec.id;
window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=122&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
}
function packinglist (){
var currentRec = currentRecord.get();
var recId = currentRec.id;
window.open("/app/accounting/print/hotprint.nl?regular=T&sethotprinter=T&formnumber=122&trantype=custinvc&&id="+recId+"&label=Invoice&printtype=transaction");
}
return {
pageInit : pageInit,
printinvoice : printinvoice,
commericalinvoice : commericalinvoice,
packinglist : packinglist
};
});