Skip to content

Instantly share code, notes, and snippets.

Last active October 3, 2023 09:16
Show Gist options
  • Save iloveitaly/a35d460b0fb744a290dce3db95873e41 to your computer and use it in GitHub Desktop.
Save iloveitaly/a35d460b0fb744a290dce3db95873e41 to your computer and use it in GitHub Desktop.
Auto-apply NetSuite Customer Payments to NetSuite Invoices when they are Created or Edited
Author: <>
Description: When is invoice is created or edited, any unapplied payments on the customer
are auto-applied to the invoice.
2. User Event
3. Name: Auto-apply Unapplied Customer Payments
4. ID: _invoice_auto_apply_payments
5. After Submit: afterSubmit
6. Deployments: Invoice. Event Type: blank. Ensure accessible to all roles and executes as admin.
// Utils
function isEmpty(obj) {
return obj === undefined || obj === null || obj === "";
function log(msg) {
nlapiLogExecution('DEBUG', msg);
function getUnappliedCustomerPaymentsForCustomer(customerInternalId) {
var filters = [
new nlobjSearchFilter('entity', null, 'anyof', customerInternalId),
new nlobjSearchFilter('mainline', null, 'is', 'T')
var results = nlapiSearchRecord('customerpayment', null, filters, null);
if(isEmpty(results)) {
log("no payments found for record: " + customerInternalId)
return 0;
var unappliedPayments = [];
for(var i = 0; i < results.length; i++ ) {
// NOTE this is a very slow operation, but there is no other way to pull the unapplied
// amount of a payment! It is not available in searches
var paymentId = results[i].getId();
var payment = nlapiLoadRecord('customerpayment', paymentId);
if(parseFloat(payment.getFieldValue('unapplied')) != 0.0) {
return unappliedPayments;
function canInvoiceBePaid(invoiceId) {
var transactionStatus = nlapiLookupField('invoice', invoiceId, 'statusRef');
// NOTE not all NetSuite accounts have a pendingApproval status
return !(transactionStatus == 'paidInFull' || transactionStatus == 'pendingApproval' || transactionStatus == 'rejected')
function afterSubmit(type) {
if(type == 'create' || type == 'edit') {
if(nlapiGetRecordType() == 'invoice') {
} else {
log("invalid record type processed: " + type)
function processInvoiceAfterSubmit(invoiceInternalId) {
if(!canInvoiceBePaid(invoiceInternalId)) {
log("invoice cannot be or is already paid");
return null;
// NOTE assumes jobs are not in use in the netsuite account
var entityId = nlapiLookupField('invoice', invoiceInternalId, 'entity');
var unappliedPayments = getUnappliedCustomerPaymentsForCustomer(entityId);
for(var i = 0; i < unappliedPayments.length; i++ ) {
var payment = unappliedPayments[i]
payment.setFieldValue('autoapply', 'T');
nlapiSubmitRecord(payment, true);
Copy link

Hi Michael, your code is working great for a customer payment, Can you please guide me how to apply a same process for journal entry.

Copy link

Thanks in advance

Copy link

Unfortunately, I don't have the time! I'd recommend joining the Slack group and asking your question there:

Copy link

Taitain commented Oct 3, 2023

Hi Michael, your code was using RESTlet API ?
I tried transform Invoice into Customer Payment using RESTlet API but I cannot create Payment applies to Invoice.
Can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment