Tim_Pedersen
11/12/2021, 3:20 PMTim_Pedersen
11/12/2021, 3:21 PMdefine(['N/currentRecord', 'N/ui/dialog', 'N/log', 'N/record'], function(currentRecord, dialog, log, record) {
/**
*@NApiVersion 2.0
*@NScriptType ClientScript
* created by Tim Pedersen Nov 10 2021
* Notes: The purpose of this script is to automatically populate the PO Type and PO Item type when a Purchase Contract is selected on the PO.
* The Purchase contract already had these fields populated there.
*/
function pageInit(context) {
log.debug("context.mode", context.mode); // If SO is being created, returns "create"; if being edited, returns "edit"
if (context.mode == 'create') { // The script will only run if a SO is being created
log.debug('pageInit', "Record being created. Script will allow copying purchase contract record.");
return;
}
// var po = context.currentRecord; // create a variable/nickname for referencing this record later on in the code
// var listedPC = po.getValue({
// fieldId: 'purchasecontract'
// });
// var pc = record.load({
// type: record.Type.PURCHASE_CONTRACT,
// id: listedPC
// });
// var itemType = pc.getValue('custbody_byu_po_item_type');
// var poType = pc.getValue('custbody_byu_po_type');
// var subsidiary = pc.getValue('subsidiary');
// var location = pc.getValue('location');
// var fundSource = pc.getValue('custbody_byu_pc_funding_source'); // the PO version of this list is 'custbody_byu_po_funding_source'. I don't know why there are 2 versions
// log.debug({
// title: 'PageInitLog ',
// details: 'Purchase Contract is ' + listedPC + ', Item Type is ' + itemType + ', and PO Type is ' + poType + ' | ' + subsidiary + ' | ' + location + ' | ' + fundSource
// });
// dialog.alert({
// title: 'PageInitAlert ',
// message: 'Purchase Contract is ' + listedPC + ', Item Type is ' + itemType + ', and PO Type is ' + poType + ' | ' + subsidiary + ' | ' + location + ' | ' + fundSource
// });
return true
}
function fieldChanged(context) {
log.debug('fieldChanged', "context is " + context.mode);
var po = context.currentRecord;
//var listedPC = po.getValue({ fieldId: 'purchasecontract' });
// todo: see if you can get past the purchase contract auto population when the vendor is selected
if (context.fieldId == 'purchasecontract') {
pcSelected = po.getValue({ fieldId: 'purchasecontract' });
if (pcSelected !== null) {
log.debug({ title: 'fieldChanged PC ID is now ', details: pcSelected });
var newpc = po.getValue({ fieldId: 'purchasecontract' });
var pc = record.load({
type: record.Type.PURCHASE_CONTRACT,
id: newpc
});
var pcitemType = pc.getValue('custbody_byu_po_item_type');
var pcpoType = pc.getValue('custbody_byu_po_type');
var subsidiary = pc.getValue('subsidiary');
var location = pc.getValue('location');
var fundSource = pc.getValue('custbody_byu_pc_funding_source');
// the PO version of the funding source list is 'custbody_byu_po_funding_source'. I don't know why there are 2 versions
if (pcitemType) {
po.setValue({ fieldId: 'custbody_byu_po_item_type', value: pcitemType })
}
if (pcpoType) {
po.setValue({ fieldId: 'custbody_byu_po_type', value: pcpoType })
}
if (subsidiary) {
po.setValue({ fieldId: 'subsidiary', value: subsidiary })
}
if (location) {
po.setValue({ fieldId: 'location', value: location })
}
if (fundSource) {
po.setValue({ fieldId: 'custbody_byu_po_funding_source', value: fundSource })
}
dialog.alert({
title: 'Automation Warning',
message: 'Po Type, Po Item Type, Subsidiary, Location, and Fund Source updated automatically based on Purchase Contract record selected.'
});
}
}
return true
};
return { // this tells NetSuite when to run each script that we created above.
pageInit: pageInit,
fieldChanged: fieldChanged
}
});Tim_Pedersen
11/12/2021, 3:35 PMTim_Pedersen
11/12/2021, 4:11 PM