Adding if statement to a Netsuite Script

Viewed 21

I am wanting to display lot numbers on item fulfilment and invoices. I don't have any scripting ability... sadly...

So I implemented this script and it works really well. https://jcurvesolutions1.zendesk.com/hc/en-us/articles/4711971574425-Remove-Bin-Number-from-item-inventorydetail-When-Printing-with-Advanced-PDF-Templates

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

    function beforeSubmit(scriptContext) {
        var rec = scriptContext.newRecord;
        var lines = rec.getLineCount({sublistId:'item'});
        var tempS = '';

        for(var i=0;i<lines;i++){ // for each line

            var subList = rec.getSublistSubrecord({
                    sublistId:'item',
                    fieldId:'inventorydetail',
                    line:i
                });

            if(subList.getValue('id')){ //if the line has inventory details
                var sublistLines = subList.getLineCount({
                        sublistId:'inventoryassignment'
                    });
                for(var j=0;j<sublistLines;j++){ //for each line of the inventory details
                    tempS+=subList.getSublistText({
                        sublistId:'inventoryassignment',
                        fieldId:'issueinventorynumber',
                        line:j
                    });
                    if(j<sublistLines-1) tempS+='\n';
                }
                rec.setSublistValue({
                    sublistId:'item',
                    fieldId:'custcol_tb_lot_no_via_js',
                    line:i,value:tempS
                });
                tempS='';
            }
        }
    }

    return {
        beforeSubmit: beforeSubmit
    };
});

However I have one issues with it. In our Netsuite account I have it set up so that Invoices display items that are on Backorder. However backorder items don't have "inventory detail" so when I try and edit or save the invoice I get the following error.

{"type":"error.SuiteScriptError","name":"USER_ERROR","message":"Invalid number (must be positive)","stack":["anonymous(N/serverRecordService)","beforeSubmit(/SuiteScripts/DisplayLot# Packing Slip.js:16)"],"cause":{"type":"internal error","code":"USER_ERROR","details":"Invalid number (must be positive)","userEvent":"beforesubmit","stackTrace":["anonymous(N/serverRecordService)","beforeSubmit(/SuiteScripts/DisplayLot# Packing Slip.js:16)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}

It is due to one of the lines not having any inventory detail as it is on back order. so what I am asking is if some kind person could tell me what I need to change in the above script so that it ignores backorder items.

Thanks.

1 Answers

To ignore backordered line items, you must get the back ordered quantity per line item. In my UI I see the field id "backordered", however in the NS records browser the field id is "quantityremaining". See which works for you.

var backOrderedQty = rec.getSublistValue({sublistId: 'item', fieldId: 'backordered', line: i}); //or try field id quantityremaining

then evaluate if it is equal to 0. If it is, then do not proceed with code for this line. If it is, then do proceed. Example:

if(backOrderedQty == 0) {
  //do nothing
} else {
  //do something
}

In your code, you already have a check for if the line has inventory details. So we can add this check for back ordered quantity to the same if statement and we do not need an else statement, since we want to "do nothing".

Full code, with modified lines not indented:

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

    function beforeSubmit(scriptContext) {
        var rec = scriptContext.newRecord;
        var lines = rec.getLineCount({sublistId:'item'});
        var tempS = '';

        for(var i=0;i<lines;i++){ // for each line

//get the current lines backordered qty
var backOrderedQty = rec.getSublistValue({
   sublistId: 'item',
   fieldId: 'backordered', //or quantityremaining
   line: i
});
            
            var subList = rec.getSublistSubrecord({
                    sublistId:'item',
                    fieldId:'inventorydetail',
                    line:i
                });

if(subList.getValue('id') && backOrderedQty ==0){ //if the line has inventory details AND back ordered qty is 0
                var sublistLines = subList.getLineCount({
                        sublistId:'inventoryassignment'
                    });
                for(var j=0;j<sublistLines;j++){ //for each line of the inventory details
                    tempS+=subList.getSublistText({
                        sublistId:'inventoryassignment',
                        fieldId:'issueinventorynumber',
                        line:j
                    });
                    if(j<sublistLines-1) tempS+='\n';
                }
                rec.setSublistValue({
                    sublistId:'item',
                    fieldId:'custcol_tb_lot_no_via_js',
                    line:i,value:tempS
                });
                tempS='';
            }
        }
    }

    return {
        beforeSubmit: beforeSubmit
    };
});
Related