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?

How can we mimic "Next Bill" button functionality using the Suite script?
805 Views Asked by kasarlaravi AtThere are 3 best solutions below
On
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();
}
});
On
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();