Automating Mass Updates in NetSuite: A Guide to Efficient Payment Record Updates

Introduction

Mass updates in NetSuite can be a powerful tool for managing large datasets efficiently. When dealing with Customer Payments, it’s essential to ensure that related records, such as Sales Rep Payment Splits, are updated correctly. This article walks through a custom NetSuite Mass Update Script that automates updates while incorporating error handling, filtering, and record skipping for optimized execution.

Use Case

The script performs the following tasks:

Retrieves Customer Payments and their linked Sales Rep Payment Split records.

Checks if the Payment Record has a PDC Received reference.

If PDC Received exists, searches for the related custom records.

If no PDC Received custom records exist, updates directly from the Payment Record.

Skips predefined Payment Record IDs to avoid unnecessary processing.

Handles missing data scenarios to prevent execution errors.

Key Features of the Script

Mass Updates Without Manual Edits: Automates updates for multiple records.

Efficient Data Lookup: Uses NetSuite Search API to fetch relevant records.

Predefined Skipping of Records: Ensures certain transactions remain untouched.

Fallback Mechanism: If PDC Received data is missing, updates records using Payment Record details.

Performance Optimization: Uses record.submitFields() instead of record.load() for faster execution.

define(['N/record', 'N/search', 'N/log'], function (record, search, log) {
    function each(params) {
        try {
            let paymentId = params.id;
            const SKIP_PAYMENT_IDS = ["19156650", "19402079", "19402485"];


            if (SKIP_PAYMENT_IDS.includes(paymentId.toString())) {
                log.debug("Skipping Payment Record", paymentId);
                return;
            }


            log.debug('Processing Payment Record', 'ID: ' + paymentId);


            let paymentSearch = search.create({
                type: "customerpayment",
                filters: [["internalid", "anyof", [paymentId]], "AND", ["mainline", "is", "T"]],
                columns: [
                    search.createColumn({ name: "internalid", join: "CUSTRECORD_VS_PAYMENTSALESREP_PA" }),
                    search.createColumn({ name: "custbody36" }) // PDC Received ID
                ]
            });


            let searchResults = paymentSearch.run().getRange({ start: 0, end: 1000 });
            if (!searchResults || searchResults.length === 0) return;


            let customRecordIds = [];
            let pdcReceivedId = null;
            searchResults.forEach(result => {
                let customRecordId = result.getValue({ name: "internalid", join: "CUSTRECORD_VS_PAYMENTSALESREP_PA" });
                if (customRecordId) customRecordIds.push(customRecordId);
                let pdcId = result.getValue("custbody36");
                if (pdcId) pdcReceivedId = pdcId;
            });


            if (customRecordIds.length === 0 && !pdcReceivedId) return;


            customRecordIds.forEach(customRecordId => {
                record.submitFields({
                    type: "customrecord_vs_sales_rep_payment_tab",
                    id: customRecordId,
                    values: { custrecord_jj_sales_rep_pa: paymentId }
                });
                log.debug("Updated Custom Record", customRecordId);
            });


        } catch (error) {
            log.error('Error Processing Payment Record', 'ID: ' + params.id + ' Error: ' + error.message);
        }
    }
    return { each: each };
});


Conclusion

This NetSuite Mass Update Script enhances efficiency and accuracy when managing Customer Payments and Sales Rep Payment Splits. By automating updates and handling edge cases, businesses can save time and reduce errors in transaction processing.

Leave a comment

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