Apply Credit Memo on specified invoice with SuiteScript.
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/record'],
function(record) {
/**
* 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: 'creditmemo',
id: creditMemoId
});
//Get line number with invoice where you want apply
var lineWithInvoice = rec.findSublistLineWithValue({
sublistId: 'apply',
fieldId: 'internalid',
value: InvoiceIdToApplyMemo
});
//Get Total ammount 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();
}
return {
onRequest: onRequest
};
});