How to create an Invoice on sales order with the next scheduled billable amount. I don't want to create an invoice with the full amount. From NetSuite UI we can use "Next Bill" button to next billable amount. How can we mimic a similar functionality using the Suite script?

- 59
- 4
3 Answers
SuiteAnswers explain that you need to set the billdate
for the invoice to be the same as the next billing date for the Sales Order. However, the examples they provide have errors and do not work.
The below snippet works as a client script in SuiteScript 1.0. You can verify by running it in the browser console while inspecting a Sales Order record. It is adapted from SuiteAnswer 63760, but the example in that answer has the incorrect date format so it fails.
var recordId = nlapiGetRecordId();
var recordType = nlapiGetRecordType();
var soRecord = nlapiLoadRecord(recordType,
recordId);
var nextBill = soRecord.getFieldValue('nextbill');
var inv = nlapiTransformRecord('salesorder', soRecord.id, 'invoice', {'billdate': nextBill});
nlapiSubmitRecord(inv,true);
For SuiteScript 2.0 the example in SuiteAnswer 70522 simply does not work. It sets the billdate
using setValue()
. Even after correcting the setValue()
statement to fieldId: 'billdate'
instead of fieldId: billdate
, the whole amount on the invoice is billed. Oleg Laskov's answer provides a working approach using defaultValues
. The below is an expanded version of Oleg's answer using moment.js for date formatting.
var oSalesOrder = scriptContext.newRecord;
var billdate = oSalesOrder.getValue({
fieldId: 'nextbill'
});
var sBillDate = moment(billdate).format('M/D/YYYY');
var invoice = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: oSalesOrder.id,
toType: record.Type.INVOICE,
isDynamic: true,
defaultValues: {
billdate: sBillDate
}
})
var invoiceId = invoice.save();

- 369
- 2
- 16
-
This was a painful one - it looks like you HAVE to set the billdate field on the invoice in the defaultValues of the transform API. I don't see why you can't just set it on the transform before calling .save() but that doesn't work. Without this answer I would have spent much longer trying to figure this out. Thanks! – csilk May 21 '21 at 00:03
var rec = record.transform({
fromType: 'salesorder',
fromId: soId,
toType: 'invoice',
isDynamic: true,
defaultValues: {
customform : customform,
billdate: trandate
}
});

- 11,891
- 53
- 45
- 80

- 11
-
-
@oleg-laskov's approach works (unlike the approach shown in SuiteAnswers). I have expanded on this answer and added a way to do this in SuiteScript 1.0 in a separate answer. – jweob Mar 29 '21 at 20:54
as per SuiteAnswers https://netsuite.custhelp.com/app/answers/detail/a_id/70522
Solution
Sales Order with a Billing Schedule can be billed using two options:
- Next Bill
- Bill Remaining
The Bill Remaining option can be achieved by transforming the Sales Order record via Script. However, to bill the Sales Order for only the amount of the next billing schedule, then the Bill Date must be set equal to the Next Bill Date of the Sales Order.
Sample script:
require(['N/record', 'N/search', 'N/log'], function(record, search, log) {
var soId = '5347';
var invoice;
var mySalesOrderSearch = search.create({
type: search.Type.SALES_ORDER,
columns: [{
name: 'nextbilldate'
}],
filters: [{
name: 'mainline',
operator: 'is',
values: ['T']
}, {
name: 'internalid',
operator: 'anyof',
values: ['soId']
}]
});
var searchResult = mySalesOrderSearch.run().getRange({
start: 0,
end: 1
});
if (searchResult != null && searchResult.length != 0) {
invoice = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: soId,
toType: record.Type.INVOICE,
isDynamic: true
});
var billdate = searchResult[0].getValue({
name: 'nextbilldate'
});
if (billdate != null && billdate != '') {
invoice.setValue({
fieldId: billdate,
value: billdate
})
}
var invoiceId = invoice.save();
}
});

- 378
- 1
- 10