Common Function for Updating NetSuite Record Fields using record.submitFields

Function to Update Record Fields

/**
 * Common function to update record fields using record.submitFields
 * @param {string} recordType - The type of the record (e.g., 'customer', 'salesorder')
 * @param {string} recordId - The internal ID of the record
 * @param {Object} fieldValues - Key-value pairs of fields to update
 * @returns {string} - The ID of the updated record
 */
function updateRecordFields(recordType, recordId, fieldValues) {
    let record = require('N/record');
    try {
        let updatedId = record.submitFields({
            type: recordType,
            id: recordId,
            values: fieldValues
        });

        log.debug('Record Updated', 'Record ID: ' + updatedId);
        return updatedId;
    } catch (error) {
        log.error('Error updating record', error);
        return null;
    }
}

Example Usage

let recordType = 'customer';
let recordId = '12345';
let fieldValues = {
    companyname: 'New Company Name',
    email: 'newemail@example.com'
};

let updatedRecordId = updateRecordFields(recordType, recordId, fieldValues);
if (updatedRecordId) {
    log.debug('Success', 'Record updated successfully: ' + updatedRecordId);
} else {
    log.error('Update Failed', 'Check the error logs for details.');
}

Leave a comment

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