Hey all! Is it possible to unapply a payment via S...
# suitescript
b
Hey all! Is it possible to unapply a payment via SuiteScript?
e
Yeah its possible. You would need to load the payment, iterate its sublist of invoices, uncheck them and save the payment record.
b
@Edgar Valdes thanks I figured it was possible, and I feel like I'm close but something just isn't working properly. Context: refunds fail to create from our billing system -> NS unless we manually unapply in NS first, trying to work around that. The (attempted) creation of the refund record has the internal id of the payment in the request so I figure, this route SHOULD work: Note: Deployment on
Customer Refund
Ignore all the debug logs, I was trying to find any point at which my code was broke lol
Copy code
/**
 * @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
    };
});