Updating a field value from another field on the save of the record

Requirement

Update the values in a new custom field from another field in the custom record for newly creating custom records and on the edit and save of the custom record.

Solution

There is a custom field named Runtime (Internal id : custrecord_au_production_runtime ) of free form type. Created another field named “Runtime Numeric“ (Internal id: custrecord_jj_runtime_numeric ) of Decimal type in the custom record. The Runtime Numeric is to store the value from the runtime field.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

define(['N/currentRecord', 'N/record', 'N/search'],
    /**
     * @param{currentRecord} currentRecord
     * @param{record} record
     * @param{search} search
     */
    (currentRecord, record, search) => {

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {

            try {
               let newRecord = scriptContext.newRecord
                let newValue = newRecord.getValue({
                    fieldId: "custrecord_au_production_runtime"
                })
                let numericValueToSet = newValue.trim()
                let numericValue = newRecord.getValue({
                    fieldId: "custrecord_jj_runtime_numeric"
                })
                log.debug("Runtime numeric value", numericValue)
                log.debug("Numeric value ", numericValueToSet)


                    if (numericValue !== numericValueToSet) {

                        let recId = record.submitFields({
                            type: 'customrecord_au_productiondata',
                            id: newRecord.id,
                            values: {
                                'custrecord_jj_runtime_numeric': parseFloat(numericValueToSet) || "0.00",
                            },
                            options: {
                                enableSourcing: false,
                                ignoreMandatoryFields: true
                            }
                        });
                        log.debug("Updated runtime numeric field......", recId)
                    }
            } catch (e) {
                log.debug("error@afterSubmit",e)

            }
        }
        return {afterSubmit}

    });

Leave a comment

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