Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active October 3, 2023 09:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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: <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);
}
}
@iloveitaly
Copy link
Author

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

http://opensuite-slackin.herokuapp.com

@Taitain
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