Mass Update Script is used to update the invoice’s due date.

The script is designed to find open invoices that are past due by 7 days and then update the due date for each of those invoices.

/**

 /**

 * @NApiVersion 2.1

 * @NScriptType MassUpdateScript

 */

 define([‘N/record’, ‘N/log’, ‘N/search’, ‘N/format’], 

 (record, log, search, format) => {

     

    const each = (params) => {

      var currentDate = new Date();

      var newDueDate = new Date(currentDate);

      newDueDate.setDate(currentDate.getDate() + 7);

      var newDueDateFormatted = format.format({

        value: newDueDate,

        type: format.Type.DATE

      });

      var invoiceSearch = search.create({

        type: search.Type.INVOICE,

        filters: [

          [‘status’, search.Operator.IS, ‘CustInvc:A’], 

          ‘AND’,

          [‘duedate’, search.Operator.BEFORE, newDueDateFormatted],

          ‘AND’,

          [‘mainline’, search.Operator.IS, ‘T’]

        ],

        columns: [‘internalid’]

      });

      invoiceSearch.run().each(function(result) {

        try {

          record.submitFields({

            type: record.Type.INVOICE,

            id: result.getValue({ name: ‘internalid’ }),

            values: {

              duedate: newDueDateFormatted

            }

          });

          log.debug({

            title: ‘Invoice Updated’,

            details: ‘Invoice ID: ‘ + result.getValue({ name: ‘internalid’ }) + ‘, New Due Date: ‘ + newDueDateFormatted

          });

        } catch (e) {

          log.error({

            title: ‘Error Updating Invoice’,

            details: ‘Invoice ID: ‘ + result.getValue({ name: ‘internalid’ }) + ‘, Error: ‘ + e.message

          });

        }

        return true;

      });

    }

    return {each}

  });

Leave a comment

Your email address will not be published. Required fields are marked *