Auto-apply NetSuite Customer Payments to NetSuite Invoices when they are Created or Edited
/* | |
Author: <mike@suitesync.io> | |
Description: When is invoice is created or edited, any unapplied payments on the customer | |
are auto-applied to the invoice. | |
Link: https://gist.github.com/iloveitaly/a35d460b0fb744a290dce3db95873e41 | |
Installation: | |
1. https://system.sandbox.netsuite.com/app/common/scripting/uploadScriptFile.nl | |
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 | |
// https://gist.github.com/iloveitaly/db7d532e772b67f5b81d0199d094301f | |
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) { | |
unappliedPayments.push(payment) | |
} | |
} | |
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') { | |
processInvoiceAfterSubmit(nlapiGetRecordId()) | |
} 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment