2
votes

I have customized a ready Script to get Lot Expiration Date,it is working fine in Edit mode for Old Records; but in Create Mode it will raise an Error here is the Code:

/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'], function (record) {


    function beforeSubmit(context) {

        log.debug({details: "beforeSubmit: " + context.type});

        var newRecord = context.newRecord;      

        // Check how many lines exist in the ItemFulfillment - Item sublist.
        var lines = newRecord.getLineCount({sublistId: 'item'});
        log.debug({details: "lines: " + lines}); // here I am Reading the Sublist Item Lines to Check how many lines there.
        if (lines > 0){
        for (var x = 0; x < lines; x++){
            var lotDetail = "";
            var newIventoryDetails = newRecord.getSublistSubrecord({
                sublistId: 'item',
                fieldId: 'inventorydetail',
                line: x
            });
            var ItemCode = newRecord.getSublistText({
                sublistId: 'item',
                fieldId: 'itemname',
                line: x
            });
            log.debug({details: "Item Code:" + ItemCode + " Line " + x});

        var newInvLines = newIventoryDetails.getLineCount({sublistId: 'inventoryassignment'});
          log.debug({details: "Inventory Details Lines = " + newInvLines + " lines"});
            if (newInvLines >0 ){
                for(z=0; z< newInvLines; z++){

                    var lotNumber = newIventoryDetails.getSublistText({   // getSublistValue
                    sublistId: 'inventoryassignment',
                    fieldId: 'issueinventorynumber',
                    line: z
                });
                var lotQty = newIventoryDetails.getSublistText({   // getSublistValue
                    sublistId: 'inventoryassignment',
                    fieldId: 'quantity',
                    line: z
                });
                var lotExpDate = newIventoryDetails.getSublistText({   // getSublistValue
                    sublistId: 'inventoryassignment',
                    fieldId: 'expirationdate',
                    line: z
                });
             lotDetail = lotDetail + " lot #: " + lotNumber + " Qty: " + lotQty + " Exp. Date:" + lotExpDate + '\n';

            }        

        }
             log.debug({details: lotDetail});
          log.debug({Line: x});
        newRecord.setSublistText({
            sublistId: 'item',
            fieldId: 'custcol_inv_full_det',
          line: x,
            text: lotDetail
       });

    }

        }
    }       


     return {
        beforeSubmit: beforeSubmit
     };
});

the error I am sure because I am updating a custom Field in Item List to Set The Text to be the full Lot Details (Lot#, Qty and Expiration Date) and Still the Main record not yet saved or created.

the Error message in Script Log

""Invalid API usage. You must use getSublistValue to return the value set with setSublistValue. ","userEvent":"beforesubmit","stackTrace":["anonymous(N/serverRecordService)","beforeSubmit(/SuiteScripts/inventoryDetails.js:45)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false} "

how to update the custom field 'custcol_inv_full_det' beforesubmit on Create Mode?

2

2 Answers

2
votes

You probably need to use 'getSublistValue' instead of 'getSublistText'. The latter may not work with all value types.

0
votes

I encountered this similar issue before. Try loading the record in dynamic mode.

var newRecord = record.load({
    type: context.newRecord.type,
    id: context.newRecord.id,
    isDynamic:true
});

... then loop through the lines and now you can use getSublistText()