How to apply a credit memo on an invoice through Suitescript

We can apply a credit memo on a specified invoice through Suitescript:

1.0

// ...existing code...
//Load Credit Memo
var creditMemoRecord = nlapiLoadRecord('creditmemo', creditMemoID);


//Get line number with invoice where you want apply
var invoiceLineIndex = creditMemoRecord.findLineItemValue('apply', 'internalid', InvoiceIdToApplyMemo);


if (invoiceLineIndex !== -1) {
    //Get Total amount of invoice
    var invoiceTotal = creditMemoRecord.getLineItemValue('apply', 'total', invoiceLineIndex);


    //Set apply to Truth (checkbox)
    creditMemoRecord.setLineItemValue('apply', 'apply', invoiceLineIndex, 'T');


    //set Payment amount - you may count how much you can apply in case credit memo < invoice
    creditMemoRecord.setLineItemValue('apply', 'amount', invoiceLineIndex, invoiceTotal);


    //save record
    nlapiSubmitRecord(creditMemoRecord);
} else {
    // Handle case where invoice is not found
    nlapiLogExecution('ERROR', 'Invoice Not Found', 'Could not find invoice line to apply credit memo.');
}
// ...existing code...

2.0

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/record', 'N/log'], function(record, log) {


  /**
   * Definition of the Suitelet script trigger point.
   *
   * @param {Object} context
   * @param {ServerRequest} context.request - Encapsulation of the incoming request
   * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
   * @Since 2015.2
   */
  function onRequest(context) {
    //+++++++++++++++
    //prior logic if exist to define which credit memo and invoice
    //+++++++++++++++


    //Load Credit Memo
    var rec = record.load({
      type: record.Type.CREDIT_MEMO,
      id: creditMemoId
    });


    //Get line number with invoice where you want apply
    var lineWithInvoice = rec.findSublistLineWithValue({
      sublistId: 'apply',
      fieldId: 'internalid',
      value: InvoiceIdToApplyMemo
    });


    if (lineWithInvoice !== -1) {
      //Get Total amount of invoice
      var totalToPay = rec.getSublistValue({
        sublistId: 'apply',
        fieldId: 'total',
        line: lineWithInvoice
      });


      //Set apply to Truth (checkbox)
      rec.setSublistValue({
        sublistId: 'apply',
        fieldId: 'apply',
        line: lineWithInvoice,
        value: true
      });


      //set Payment amount - you may count how much you can apply in case credit memo < invoice
      rec.setSublistValue({
        sublistId: 'apply',
        fieldId: 'amount',
        line: lineWithInvoice,
        value: totalToPay
      });


      //save record
      rec.save();
    } else {
      // Handle case where invoice is not found
      log.error({
        title: 'Invoice Not Found',
        details: 'Could not find invoice line to apply credit memo.'
      });
    }
  }


  return {
    onRequest: onRequest
  };


});

For more details refer the article: Apply Credit Memo via SuiteScript https://suiteanswers.custhelp.com/app/answers/detail/a_id/82839

Leave a comment

Your email address will not be published. Required fields are marked *