How would you get values from header fields and th...
# suitescript
n
How would you get values from header fields and then use those to default when a new line on the item sublist is added
b
Client script has lineInit entry point
n
@barinvon I couldn’t get it to trigger anything when I added a new line
b
want to post the code? you would use getValue to get the header value and then setCurrentSublistValue on lineInit
n
Copy code
/**
  *@NApiVersion 2.0
  *@NScriptType ClientScript
  */
define(['N/ui/dialog', 'N/search', 'N/currentRecord'],
    function (dialog, search, curRec) {

        function pageInit(scriptContext) {
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         * @param scriptContext.fieldId
         * @param scriptContext.line
         */
        function fieldChanged(scriptContext) {
            if (scriptContext.sublistId === 'item' || scriptContext.sublistId === 'expense' && scriptContext.fieldId === 'custcol_psc_location_line') {
                scriptContext.currentRecord.selectLine({
                    sublistId: 'item',
                    line: scriptContext.line
                });
                var pscLocationLine = scriptContext.currentRecord.getCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_psc_location_line'
                });

                var locationName = '';

                if (pscLocationLine) {
                    locationName = search.lookupFields({
                        type: 'customrecord_psc_cc_loc_combos',
                        id: pscLocationLine,
                        columns: 'custrecord_psc_cclc_location'
                    });
                }

                scriptContext.currentRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'location',
                    value: locationName ? locationName.custrecord_psc_cclc_location[0].value : '',
                    ignoreFieldChange: true
                });
            }
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         */
        function lineInit(scriptContext) {
            var myRecord = curRec.get();
            var pscCostCenter = myRecord.getValue({
                fieldId: 'department'
            });

            var location = myRecord.getValue({
                fieldId: 'custbody_psc_location'
            });

            if (scriptContext.sublistId == 'item' || scriptContext.sublistId == 'expense') {
                if (pscCostCenter) {
                    scriptContext.currentRecord.setCurrentSublistValue({
                        sublistId: scriptContext.sublistId,
                        fieldId: 'department',
                        value: pscCostCenter,
                        ignoreFieldChange: false,
                    });
                }

                if (location) {
                    scriptContext.currentRecord.setCurrentSublistValue({
                        sublistId: scriptContext.sublistId,
                        fieldId: 'custcol_psc_location_line',
                        value: location,
                        ignoreFieldChange: false,
                    });
                }
            }
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         * @param scriptContext.fieldId
         */
        function postSourcing(scriptContext) {
            
        }
        function saveRecord(scriptContext) {

        }
        function sublistChanged(scriptContext) {
        }
        function validateDelete(scriptContext) {

        }
        function validateField(scriptContext) {

        }
        function validateInsert(scriptContext) {   
        }
        return {
            // pageInit: pageInit,
            fieldChanged: fieldChanged,
            lineInit: lineInit,
            // postSourcing: postSourcing,
            // saveRecord: saveRecord,
            // sublistChanged: sublistChanged,
            // validateDelete: validateDelete,
            // validateField: validateField,
            // validateInsert: validateInsert
        };
    });
I tried using the get.currentRecord and just normal scriptContext.currentRecord.getValue and neither of them did anything on line init. it did work when I tried to edit an existing line but not when trying to add a new line
b
Id use var
myRecord = scriptContext.currentRecord;
instead of curRec.get() and I like to put a console log in pageInit and use browser developer tools to open script and debug, see if you are getting those values
n
So i did that and get the value on pageInit. Then I put this in lineinit and nothing
Copy code
var myRecord = scriptContext.currentRecord;
            var pscCostCenter = myRecord.getValue({
                fieldId: 'department'
            });

            var location = myRecord.getValue({
                fieldId: 'custbody_psc_location'
            });

            console.log(pscCostCenter)
b
are you creating a record and then selecting the line that is already there? If that is the case I don't think lineInit will fire, I'd do a field change on department and then get line count of item sublist and if that is zero use selectLine to select line zero and set the values
n
So in my example there is one item already in the list. Then I am going to the next line and am selecting a item. After i select that item it should then set the values of department and location.
b
share more code, i cant tell what entry point you are attempting this on, nor which suitescript version
n
@battk here is the code
Copy code
/**
  *@NApiVersion 2.0
  *@NScriptType ClientScript
  */
define(['N/ui/dialog', 'N/search', 'N/currentRecord'],
    function (dialog, search, curRec) {

        function pageInit(scriptContext) {
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         * @param scriptContext.fieldId
         * @param scriptContext.line
         */
        function fieldChanged(scriptContext) {
            if (scriptContext.fieldId == 'item') {
                
                var myRecord = scriptContext.currentRecord;
                var pscCostCenter = myRecord.getValue({
                    fieldId: 'department'
                });

                var location = myRecord.getValue({
                    fieldId: 'custbody_psc_location'
                });

                if (pscCostCenter) {
                    myRecord.setCurrentSublistValue({
                        sublistId: scriptContext.sublistId,
                        fieldId: 'department',
                        value: pscCostCenter,
                        ignoreFieldChange: true,
                    });
                }

                if (location) {
                    myRecord.setCurrentSublistValue({
                        sublistId: scriptContext.sublistId,
                        fieldId: 'custcol_psc_location_line',
                        value: location,
                        ignoreFieldChange: true,
                    });
                }

            } 
            // if (scriptContext.fieldId === 'custcol_psc_location_line') {
            //     scriptContext.currentRecord.selectLine({
            //         sublistId: 'item',
            //         line: scriptContext.line
            //     });
            //     var pscLocationLine = scriptContext.currentRecord.getCurrentSublistValue({
            //         sublistId: 'item',
            //         fieldId: 'custcol_psc_location_line'
            //     });

            //     var locationName = '';

            //     if (pscLocationLine) {
            //         locationName = search.lookupFields({
            //             type: 'customrecord_psc_cc_loc_combos',
            //             id: pscLocationLine,
            //             columns: 'custrecord_psc_cclc_location'
            //         });
            //     }

            //     scriptContext.currentRecord.setCurrentSublistValue({
            //         sublistId: 'item',
            //         fieldId: 'location',
            //         value: locationName ? locationName.custrecord_psc_cclc_location[0].value : '',
            //         ignoreFieldChange: true
            //     });
            // }
            
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         */
        function lineInit(scriptContext) {
            var myRecord = scriptContext.currentRecord;
            var pscCostCenter = myRecord.getValue({
                fieldId: 'department'
            });

            var location = myRecord.getValue({
                fieldId: 'custbody_psc_location'
            });

            if (pscCostCenter) {
                myRecord.setCurrentSublistValue({
                    sublistId: scriptContext.sublistId,
                    fieldId: 'department',
                    value: pscCostCenter,
                    ignoreFieldChange: true,
                });
            }

            if (location) {
                myRecord.setCurrentSublistValue({
                    sublistId: scriptContext.sublistId,
                    fieldId: 'custcol_psc_location_line',
                    value: location,
                    ignoreFieldChange: true,
                });
            }
        }

        /**
         * 
         * @param scriptContext 
         * @param scriptContext.currentRecord
         * @param scriptContext.sublistId
         * @param scriptContext.fieldId
         */
        function postSourcing(scriptContext) {

        }
        function saveRecord(scriptContext) {

        }
        function sublistChanged(scriptContext) {
        }
        function validateDelete(scriptContext) {

        }
        function validateField(scriptContext) {

        }
        function validateInsert(scriptContext) {
        }
        return {
            //pageInit: pageInit,
            fieldChanged: fieldChanged,
            lineInit: lineInit,
            // postSourcing: postSourcing,
            // saveRecord: saveRecord,
            // sublistChanged: sublistChanged,
            // validateDelete: validateDelete,
            // validateField: validateField,
            // validateInsert: validateInsert
        };
    });
Really just need to default a couple fields based on header level fields on new lines
b
lineInit is when you select a line on your sublist
for an item sublist, its inappropriate to default department
netsuite sources the department from the item to the department on the line
whatever value you are trying to put in the line will be overwritten
n
so if you were going to default a couple fields when creating a new line how would you do it? Because they want to default department based off the department on the header level
b
same thing for custcol_psc_location_line if the field is setup to do sourcing
you would want to set the department after the sourcing for the item is complete
n
So, either way i can't default it? Cause when I add an item now, sometimes it fills in department and sometimes not. So. id want to default it when empty
So you'd do that in the postSourcing function"
?
b
fieldChanged is inappropriate for that task, it occurs at the same time that the fields are being sourced
you are correct in guessing that you want postSourcing
n
i don't see scriptContext.line on the postSourcing documentation. How would you set the line you are editing?
Or just look for id of field and sublist and then setCurrentSublistValue?
b
you dont really have a choice in the matter
the only way to set sublist fields in ss2 is setCurrentSublistValue
n
I see. What about once I do that, if I go to edit department and change it, is it going to default again and it will just be stuck in a loop to always default?
b
depends on how you write it
if you go with the version you have in fieldChanged, your if statement at the top means it only runs when the fieldId is item
n
Yep that might work. I’ll try a little later. Thanks foe the help @battk