ANS-0966 · SUITESCRIPT DEVELOPMENT

How to Scriptmatically Generate the Next Invoice from a NetSuite Sales Order with a Billing Schedule

Automate the generation of the next invoice or cash sale from a NetSuite sales order using SuiteScript 2.x, leveraging its billing schedule.

Short answer

To generate the next invoice or cash sale from a sales order with a billing schedule, use SuiteScript 2.x to load the sales order, retrieve the 'nextbill' date, transform the sales order into the desired transaction type, set the 'billdate' field on the transformed record, and then save it.

Scenario

NetSuite users often need to programmatically generate the next invoice or cash sale for sales orders that have an associated billing schedule. This process ensures that subsequent billing events are accurately triggered and recorded, maintaining the integrity of recurring revenue streams.

Solution

To programmatically generate the next invoice or cash sale from a sales order with an active billing schedule, SuiteScript 2.x can be utilized to transform the sales order record. The key is to retrieve the nextbill date from the original sales order and apply it as the billdate during the transformation process.The following SuiteScript 2.x code demonstrates this process:

javascript
// Assume salesOrderId is the internal ID of the sales order
var salesOrderRecord = record.load({
    type: record.Type.SALES_ORDER,
    id: salesOrderId,
    isDynamic: false
});

var nextBillDateValue = salesOrderRecord.getValue({ fieldId: 'nextbill' });

var transformedRecord = record.transform({
    fromType: record.Type.SALES_ORDER,
    fromId: salesOrderId,
    toType: record.Type.CASH_SALE, // Use record.Type.INVOICE for an Invoice
    isDynamic: false
});

if (nextBillDateValue) {
    transformedRecord.setValue({
        fieldId: 'billdate',
        value: nextBillDateValue
    });
}

transformedRecord.save();

Considerations for implementation:

  • This script assumes a billing schedule is set on the sales order. Implement validation to ensure a billing schedule exists before attempting the transformation.
  • The toType parameter in record.transform can be set to record.Type.INVOICE for an invoice or record.Type.CASH_SALE for a cash sale, depending on the required transaction type.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant