Search code examples
netsuitesuitescript2.0

SuiteScript 2.X Client Script Set Sublist Value not working


I'm attempting to uncheck a checkbox on the Sales Order transaction during line item entry if the location is a specific ID, I have tested with console.log() and confirmed I'm able to see the value, but for some reason when I attempt to set the value it won't work.

I've tried a number of recommended answers but nothing has worked so far, here is my script:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/action', 'N/currentRecord', 'N/record', 'N/transaction'],
/**
 * @param{action} action
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{transaction} transaction
 */
function(action, currentRecord, record, transaction) {
    
    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function sublistChanged(scriptContext) {
        var recLoc = scriptContext.currentRecord.getValue('location');

        if(recLoc == 79) {
            var line = nlapiGetLineItemCount('item');

            nlapiSetLineItemValue('item','createwo',line,'F');
        };
    }

    return {
        sublistChanged: sublistChanged
    };
    
});

I appreciate any insight!

Update

I've adjusted my script using Karl-Henry Martinsson's suggestions but I'm still unable to uncheck the checkbox, here is my new code:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/action', 'N/currentRecord', 'N/record', 'N/transaction'],
/**
 * @param{action} action
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{transaction} transaction
 */
function(action, currentRecord, record, transaction) {
    
    /**
     * Function to be executed after sublist is inserted, removed, or edited.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.currentRecord - Current form record
     * @param {string} scriptContext.sublistId - Sublist name
     *
     * @since 2015.2
     */
    function sublistChanged(scriptContext) {
        var currRec = scriptContext.currentRecord;
        var sublistId = scriptContext.sublistId;
        var recLoc = currRec.getValue({
            fieldId: 'location'
        });
        
        if(recLoc === '79') {
            if(sublistId === 'item') {
                console.log('success');

                currRec.setCurrentSublistValue({
                    sublistId: sublistId,
                    fieldId: 'createwo',
                    value: false,
                    ignoreFieldChange: true
                });
            };
        };
    }

    return {
        sublistChanged: sublistChanged
    };
    
});


Solution

  • Two things I noticed about your script:

    1. As already noted by Karl, you are using SuiteScript 1.0 functions within a SuiteScript 2.x script. In a client script, the functions will be available, but they aren't guaranteed to be compatible.
    2. The value of your variable line is being set to the line count of the sublist you're targeting. This means that in a 1-indexed API like SS1.0 you would always be targeting the last line in the sublist, or with a 0-indexed API like SS2.0 you would be targeting one line beyond the end (IE: a non-existent line). Not sure what happens with a SS1.0 function inside a SS2.0 script.

    I'm also not sure that sublistChanged() is the best event to trigger this on. From what I've seen, this does not work as you'd expect for your scenario, because it will be triggered after the line is committed, so by the time the function runs, the "current line" will have changed.

    Another consideration is at what point you want to make the decision to unset the createwo field, and whether NetSuite field sourcing has any effect. The createwo field is sourced when the item is added, based on the setting of the "Special Work Order Item" setting on the item record. If you set the value before the sourcing occurs, is will be overwritten.

    To address these issues, you can use the postSourcing() entry point to check and update the field immediately after the item is entered:

    function postSourcing(context) {
        if (context.sublistId == 'item' && context.fieldId == 'item') {
            var recLoc = context.currentRecord({
                fieldId: 'location'
            });
            if (recLoc == 79) {
                context.currentRecord.setCurrentSublistValue({
                    fieldId: 'createwo',
                    sublistId: 'item',
                    value: false
                });
            }
        }
    }
    

    You could alternatively use validateLine to check and update the value immediately before the line is committed - when you click Add on each line instead of immediately after you set the Item.