Ben Barnett
12/26/2024, 6:21 PMEdgar Valdes
12/26/2024, 10:14 PMBen Barnett
12/26/2024, 10:19 PMCustomer Refund
Ignore all the debug logs, I was trying to find any point at which my code was broke lol
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/log'], (record, log) => {
const beforeSubmit = (context) => {
log.debug('Script Start', 'beforeLoad triggered');
//if (context.type !== context.UserEventType.CREATE) {
// log.debug('Context Type Check', `Context type is not CREATE. Current type: ${context.type}`);
// return;
// }
const refund = context.newRecord;
// log.debug('Refund Record Loaded', `Refund ID: ${refund.id}`);
try {
// Debug available sublists
const sublists = refund.getSublists();
log.debug('Available Sublists', sublists);
// Debug fields in the 'apply' sublist
const applySublistFields = refund.getSublistFields({ sublistId: 'apply' });
log.debug('Apply Sublist Fields', applySublistFields);
// Get line count of the 'apply' sublist
const lineCount = refund.getLineCount({ sublistId: 'apply' });
log.debug('Apply Sublist Line Count', `Number of lines: ${lineCount}`);
// Loop through each line to find values in the 'doc' field
for (let i = 0; i < lineCount; i++) {
const docValue = refund.getSublistValue({
sublistId: 'apply',
fieldId: 'doc',
line: i
});
log.debug(`Line ${i} Doc Value`, `Doc: ${docValue}`);
}
// Attempt to get value from the first line directly
const applyList = refund.getSublistValue({
sublistId: 'apply',
fieldId: 'doc',
line: 0
});
log.debug('Apply Sublist Value', `Apply List Value: ${applyList}`);
if (applyList) {
// Load associated payment
const payment = record.load({
type: 'customerpayment',
id: applyList
});
log.debug('Payment Record Loaded', `Payment ID: ${applyList}`);
// Check if payment is applied to an invoice
const isApplied = payment.getValue({ fieldId: 'applied' });
log.debug('Payment Applied Check', `Is Payment Applied: ${isApplied}`);
if (isApplied) {
// Unapply the payment
payment.setValue({ fieldId: 'applied', value: false });
payment.save();
log.audit('Payment Unapplied', `Payment ID: ${applyList}`);
} else {
log.debug('Payment Not Applied', `No unapplied action needed for Payment ID: ${applyList}`);
}
} else {
log.debug('No Associated Payment', 'No payment found in apply sublist.');
}
} catch (error) {
log.error({
title: 'Error processing refund',
details: error
});
}
};
return {
beforeSubmit
};
});