Hello scripters, hopefully you can help. I am atte...
# suitescript
s
Hello scripters, hopefully you can help. I am attempting to remove promotions from the promotions subtab on an invoice via a script. I am doing this instead of removing it from the items subtab because the Sales Promotion item cannot be edited as it is tied to the item. However, I am getting an error stating that I cannot perform this operation. See thread for code and error. I appreciate any insight you can provide.
Full script, which includes some other actions that are working.
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
define(['N/search', 'N/record'],

    function (search, record) {

        function execute(context) {
            try {
                var wsiInvoicesSearch = search.load({id: 'customsearch_wsi_invoices_in_error_test'})
                var wsiInvoicesSearchResults = wsiInvoicesSearch.run()

                wsiInvoicesSearchResults.each(function(invoice){
                    var invoiceId = invoice.id

                    var loadedInvoice = record.load({
                        type: record.Type.INVOICE,
                        id: invoiceId,
                        isDynamic: true
                    })
                    
                    loadedInvoice.setValue({
                        fieldId: 'automaticallyapplypromotions',
                        value: false
                    })

                    var promotionsLineCount = loadedInvoice.getLineCount({sublistId: 'promotions'})

                    for (var i = promotionsLineCount; i > 0; i--) {
                      log.debug("promo line", 
                      loadedInvoice.selectLine({
                          sublistId: 'promotions',
                          line: i
                      }))
                            loadedInvoice.removeLine({
                            sublistId: 'promotions',
                            line: i
                        })
                    }
                    
                    var itemLineCount = loadedInvoice.getLineCount({sublistId: 'item'})

                    for (var i = 0; i < itemLineCount; i++) {

                        loadedInvoice.selectLine({
                            sublistId: 'item',
                            line: i
                        })

                        var itemType = loadedInvoice.getCurrentSublistValue({
                          sublistId: 'item',
                          fieldId: 'itemtype'
                      })

                      if (itemType != 'Discount') {

                        var itemDisplay = loadedInvoice.getCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'item_display'
                        })
                        
                        var tpPrice = loadedInvoice.getCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'custcol_sps_purchaseprice'
                        })

                        loadedInvoice.setCurrentSublistValue({
                                sublistId: 'item',
                                fieldId: 'rate',
                                line: i,
                                value: tpPrice
                            })

                        loadedInvoice.commitLine({
                            sublistId: 'item',
                            line: i
                        })

                        log.debug('tp price', tpPrice)
                        log.debug('item display', itemDisplay)
                    }
                  }
                     var integrationStatus = loadedInvoice.getValue({
                        fieldId: 'custbodyintegrationstatus'
                    })
                    log.debug('integration status', integrationStatus)

                    loadedInvoice.save()

                    return true;
                })
            } catch (error) {
                log.error("Error in Scheduled Script", error.toString());
            }
        }

        return {
            execute: execute
        };
    });
Error
Copy code
{
  "type": "error.SuiteScriptError",
  "name": "SSS_INVALID_SUBLIST_OPERATION",
  "message": "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.",
  "id": "",
  "stack": [
    "Error\n at RecordInvoker.removeLine (suitescript/resources/javascript/record/serverRecordService.js:333:5)\n at NetSuiteObject.thenableFunction() (suitescript/resources/javascript/record/proxy.js:115:24)\n at /SuiteScripts/fix-wsi-invoices-scheduled.js:37:43\n at Object.execute (/SuiteScripts/fix-wsi-invoices-scheduled.js:15:42)"
  ],
  "cause": {
    "type": "internal error",
    "code": "SSS_INVALID_SUBLIST_OPERATION",
    "details": "You have attempted an invalid sublist or line item operation. You are either trying to access a field on a non-existent line or you are trying to add or remove lines from a static sublist.",
    "userEvent": null,
    "stackTrace": [
      "Error\n at RecordInvoker.removeLine (suitescript/resources/javascript/record/serverRecordService.js:333:5)\n at NetSuiteObject.thenableFunction() (suitescript/resources/javascript/record/proxy.js:115:24)\n at /SuiteScripts/fix-wsi-invoices-scheduled.js:37:43\n at Object.execute (/SuiteScripts/fix-wsi-invoices-scheduled.js:15:42)"
    ],
    "notifyOff": false
  },
  "notifyOff": false,
  "userFacing": true
}
a
not sure if this is the issue, but you're going through the line last to first... you need to start at linecount -1, not linecount
Copy code
for (var i = promotionsLineCount; i > 0; i--) {...}
should be rewritten as
Copy code
for (var i = promotionsLineCount-1; i >= 0; i--) {...}
need to change the i > 0 to i>=0 too
at the risk of sounding patronizing... arrays start at 0 not 1, whereas the linecount result will just count the lines starting at 1 like a human so your last line index is actually linecount -1 not linecount
s
@Anthony OConnor thanks, that was code added by someone else who tried working on it. I didn't notice that he started from the last line. Very strange.
In any case, that is not the cause of the issue.
a
whenyou remove lines you HAVE to go backwards, otherwise the line numbers change and your loop makes zero sense
s
Ah, yes. That makes total sense. Thank you
👍 1
e
Or you can always remove line 0