I'm having difficulty working around a record fiel...
# suitescript
t
I'm having difficulty working around a record field that is being manipulated by other actions in NetSuite. I'm working with Purchase Orders, and I want to populate certain fields based on the "Purchase Contract" (purchasecontract) field. There are 5 or so fields on associated purchase contract that have identical custom fields on the purchase order, and right now we are having to enter them manually in both places. In my attempts to use a client script to populate those fields, I created a 'fieldChanged' function for that entry point. However, the purchase contract field on the purchase order also is being manipulated by NetSuite to only show applicable purchase contracts for that vendor (as opposed to showing all purchase contracts - which is a positive). As I attempt to trigger behavior whenever that field is changed, either the field becomes disabled and grayed out, or the field resets itself to blank after the function has run its course. I'd welcome any insight or wisdom as to why. Code in the thread - >
define(['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     } });
When the Vendor is selected on the purchase order the purchase contract options are loaded which triggers the fieldChanged entry point.
I got this figured out. I changed my if statement to only trigger if the purchasecontract was > 0. This increased selectivity did the trick.