Last active
August 1, 2024 08:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} | |
} |
Thanks in advance
Unfortunately, I don't have the time! I'd recommend joining the Slack group and asking your question there:
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
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.