2

I have created a pageInit SuiteScript that should remove line items on a Sales Order if they have a custom field filled out. The issue I am having is that it will not remove the line if it is the last line and no other line has a value for loss.

function pageInit(context) {

    var objRec = context.currentRecord;

    var itemsLength = objRec.getLineCount({
        sublistId: 'item'
    });

    for (var i = itemsLength-1; i >= 0; i--){
        var loss = objRec.getSublistValue({
            sublistId: 'item',
            fieldId: 'custcol_linelossreason',
            line: i
        });

        if (loss) {
            objRec.removeLine({
                sublistId: 'item',
                line: i,
                ignoreRecalc: true
            });
            log.debug('removed', 'Line ' + i + ' has been removed.');
        } else {
            log.debug('no removal', 'Line ' + i + ' will remain.');
        }
    }

1 Answers1

1

The only thing I can find is that maybe that line is missing the loss value...? I tested with your code, minus the loss piece and it worked perfectly. Below is what I tried with.

**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 * 
 */
define(['N/record'],function(record){
  function pageInit(context){
    var objRec=context.currentRecord;
    var itemsLength=objRec.getLineCount({
      sublistId:'item'
    });
    log.debug('itemsLength',itemsLength);
    for(var i=itemsLength-1;i>=0;i--){
      log.debug('running line ',i+' of '+itemsLength);
      /*
      var loss=objRec.getSublistValue({
        sublistId:'item',
        fieldId:'custcol_linelossreason',
        line: i
      });
      */
      //if(loss){
      objRec.removeLine({
        sublistId:'item',
        line:i,
        ignoreRecalc:true
      });
      log.debug('removed','Line '+i+' has been removed.');
     /*
      }else{
        log.debug('no removal','Line '+i+' will remain.');
      }
      */
    }
  }

  return{
    pageInit:pageInit
  }
});
w3bguy
  • 2,215
  • 1
  • 19
  • 34
  • 1
    Thank you for your response! Your code did not work in my environment which made me dig deeper. Turns out I had two instances of this same script running so they were conflicting with one another. – Jessica Lane Jul 09 '20 at 15:39
  • Yep, that'll definitely do it. :) Glad you found the issue! FYI, if you have not already, make sure to check out the NetSuite Professionals Slack. :) – w3bguy Jul 09 '20 at 15:53