write a script that applies a credit memo to an in...
# ask-stanley-ai
m
write a script that applies a credit memo to an invoice
c
AI generated answer (using internal Slack threads):
****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ****** Yes, it is possible to write a script that applies a credit memo to an invoice. You can use the
record.transform
method in SuiteScript to transform an invoice into a customer payment, which can then be used to apply the credit memo. Here is a simplified example:
Copy code
javascript
var invoiceId = '123'; // replace with your invoice internal id
var creditMemoId = '456'; // replace with your credit memo internal id

var customerPayment = record.transform({
    fromType: record.Type.INVOICE,
    fromId: invoiceId,
    toType: record.Type.CUSTOMER_PAYMENT,
    isDynamic: true,
});

var lineCount = customerPayment.getLineCount({ sublistId: 'apply' });
for (var i = 0; i < lineCount; i++) {
    customerPayment.selectLine({ sublistId: 'apply', line: i });
    var applyInvoiceId = customerPayment.getCurrentSublistValue({ sublistId: 'apply', fieldId: 'internalid' });
    if (applyInvoiceId == invoiceId) {
        customerPayment.setCurrentSublistValue({ sublistId: 'apply', fieldId: 'apply', value: true });
        break;
    }
}

lineCount = customerPayment.getLineCount({ sublistId: 'credits' });
for (var i = 0; i < lineCount; i++) {
    customerPayment.selectLine({ sublistId: 'credits', line: i });
    var applyCreditMemoId = customerPayment.getCurrentSublistValue({ sublistId: 'credits', fieldId: 'internalid' });
    if (applyCreditMemoId == creditMemoId) {
        customerPayment.setCurrentSublistValue({ sublistId: 'credits', fieldId: 'apply', value: true });
        break;
    }
}

customerPayment.save();
This script first transforms the invoice into a customer payment. It then loops through the 'apply' sublist to find the invoice and sets the 'apply' field to true. It does the same for the 'credits' sublist to apply the credit memo. Finally, it saves the customer payment record, which applies the credit memo to the invoice. Please replace
'123'
and
'456'
with your actual invoice and credit memo internal ids.
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.866076827
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.85347712
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.838887095
Please provide your feedback on this answer.