'Cannot get sublist field to change using setCurrentSublistValue

I have a script that uses sublistChanged to monitor an inline sublist in a sales order.

The sublist has 3 fields: Quantity Weight Total Weight (is equal to quantity * weight)

What I am trying to do is - if the quantity is updated that the total weight is automatically updated using sublistChanged.

In the console I can see the math working but when it comes time to updating the field it does not.

What am I missing?

    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
define(['N/record'], function (r) {
    function sublistChanged(context) {
        var currentRecord = context.currentRecord;
        var sublistName = context.sublistId;
        var quantity = currentRecord.getCurrentSublistValue({
            sublistId: 'recmachcustrecord_dims',
            fieldId: 'custrecord_quantity'
        });
        var weight = currentRecord.getCurrentSublistValue({
            sublistId: 'recmachcustrecord_dims',
            fieldId: 'custrecord_weight'
        });


        var op = context.operation;
        if (sublistName == 'recmachcustrecord_dims')
        console.log("Recalc Quantity is " + quantity);
        console.log("Recalc Weight is " + weight);
        var recalcTotalWeight = quantity * weight;
        console.log("New Recalc Total Weight is " + recalcTotalWeight);
        currentRecord.setCurrentSublistValue({
            sublistId: "recmachcustrecord_dims", 
            fieldId: "custrecord_total_weight", 
            value: recalcTotalWeight,
            forceSyncSourcing: true

        });
    }
return {
    sublistChanged: sublistChanged
};
    });


Solution 1:[1]

When working with "currentRecord" the record is considered as "dynamic". For more information about SuiteScript 2.x Standard and Dynamic Modes, reference Suite Answer 73792. When in dynamic mode to set a sublist value you must do so in 3 steps:

currentRecord.selectLine({
  sublistId: 'recmachcustrecord_dims',
  line: 0 //0 is the first line
});
currentRecord.setCurrentSublistValue({
  sublistId: "recmachcustrecord_dims", 
  fieldId: "custrecord_total_weight", 
  value: recalcTotalWeight,
  forceSyncSourcing: true
});
currentRecord.commitLine({
  sublistId: 'recmachcustrecord_dims'
});

This specific 3 steps are referenced in Suite Answer 45396

Solution 2:[2]

I had faced a similar situation before. Apparently, the sublists don't get updated using currentRecord.setCurrentSublistValue.For getting and setting values on a sublist, you need to load the record and then set the sublist values. Pasting here my sample in SuiteScript 2.0 where I am getting contract asset id from a sales order's line and setting it on the invoice lines:

//Get the sales order Id from the created from field
        var SalesOrderID = CurrentRecord.getValue('createdfrom');
        log.debug('SalesOrderID',SalesOrderID); 
        
        if(SalesOrderID){
        //Load Sales Order to get SFDC contract Asset ID
        var SalesOrderRecord = record.load({
                type: record.Type.SALES_ORDER,
                id: SalesOrderID,
                isDynamic: true,
            });
            
        var InvoiceRecord = record.load({
                type: record.Type.INVOICE,
                id: CurrentRecordID,
                isDynamic: true,
            });
        log.debug('InvoiceRecord',InvoiceRecord);
        
        var LineCount = SalesOrderRecord.getLineCount('item');
        log.debug('Line Count is:',LineCount);
        
        for(var LineNo = 0; LineNo < LineCount; LineNo++){
            log.debug('Line Number is:',LineNo);
            
            var SFDCContractAssetID = SalesOrderRecord.getSublistValue({
                sublistId: 'item', 
                fieldId: 'custcol_sfdc_contract_asset_id', 
                line: LineNo
            });
            log.debug('SFDCContractAssetID is:',SFDCContractAssetID);
            
            if(SFDCContractAssetID){
            InvoiceRecord.selectLine({
                sublistId : 'item',
                line : LineNo
            });
            
            InvoiceRecord.setCurrentSublistValue({
                sublistId: 'item', 
                fieldId: 'custcol_sfdc_contract_asset_id', 
                value: SFDCContractAssetID
                //ignoreFieldChange: true
            });
            InvoiceRecord.commitLine({sublistId: 'item'});
            }
        }//end of loop
        InvoiceRecord.save();
        log.debug('All Invoice Lines updated','All Invoice Lines Updated');     
        }

Please try this and let me know if this works!!!

Solution 3:[3]

This could be a late answer but how about using the fieldChanged not sublistChanged?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martha
Solution 2 Anuradha Sinha
Solution 3 ouflak