1
votes

I have create a simple ClientScript to update a sales order sublist value from item fulfillment.
when the script run the record.save() API, I got error JS_EXCEPTION - TypeError: Cannot read property 'name' of undefined.

Here Is my Script :

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(['N/record', 'N/currentRecord', 'N/search', 'N/log'], function (record, currentRecord, search, log) {
    function saveRecord(context) {
        var currentRecord = context.currentRecord;

        var soId = currentRecord.getValue({
            fieldId: 'createdfrom'
        });

        var soRecord = record.load({
            type: record.Type.SALES_ORDER,
            id: soId,
            isDynamic: true
        });

        var soItemCount = soRecord.getLineCount({
            sublistId: 'item'
        });
        log.debug({
            title: "soItemCount",
            details: soItemCount
        });

        for (var so = 0; so < soItemCount; so++) {
            soRecord.selectLine({
                sublistId: 'item',
                line: so
            });

            soRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_need_approval',
                value: true
            });

            soRecord.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'custcol_fulfill_qty',
                value: 90
            });
        }
        soRecord.commitLine({
            sublistId: 'item'
        });

    soRecord.save();

    return true;
}
    return {
    saveRecord: saveRecord
};
});

can anyone give me a suggestion with my script ?

2

2 Answers

1
votes

Can you check if the error is form any other UserEvent script deployed on Sales order, as when you try to save the record with client script all User event scripts deployed will execute.

Try undeploying other scripts and test once to rule out error

0
votes

Probably the best approach to the solution you want to make is a UserEvent with an afterSubmit event.

The solution code is going to be very similar.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search', 'N/log'], 
    function (record, search, log) {
        function afterSubmit(context) {
            var currentRecord = context.newRecord;

            var soId = currentRecord.getValue({
                fieldId: 'createdfrom'
            });

            var soRecord = record.load({
                type: record.Type.SALES_ORDER,
                id: soId,
                isDynamic: true
            });

            var soItemCount = soRecord.getLineCount({
                sublistId: 'item'
            });

            log.debug({
                title: "soItemCount",
                details: soItemCount
            });

            for (var so = 0; so < soItemCount; so++) {
                
                soRecord.setSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_need_approval',
                    line: so,
                    value: true
                });

                soRecord.setSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_fulfill_qty',
                    line: so,
                    value: 90
                });
                
            }       

            soRecord.save();
        }
    return {
        afterSubmit: afterSubmit
    };
});