Reducing the Recurrence count by one whenever a user creates a copy of the PO record in Before Submit Trigger using User Event Script in Netsuite.

Steps:-

  1. First visit a Parent PO record.
  2. Click on edit and enter a desired positive integer value in the Number of Recurrence field.
  3. Click on save.
  4. Make a copy of the Parent PO.
  5. Save the child record or copy of that PO.
  6. Navigate to the respective or related Parent PO.
  7. The number will be reduced by one compared to the before value.
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*******************************************************************************
 * CLIENT NAME:J&J
 * JJ-11
 * JJ Project
 *******************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description :
 * To apply internal control and the script that fills in the following fields when the user triggers the Make Copy
 * Date created :21-MARCH-2022
 * Created by: NANDESH, Jobin & Jismi IT Services LLP
 * REVISION HISTORY
 * Revision 2.0
 *
 ******************************************************************************/
define(['N/record'],
    /**
     * @param{record} record
     */
    (record) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            if (scriptContext.type==='copy'){
                const parentRecordId = scriptContext.request.parameters.id;
                scriptContext.newRecord.setValue({
                    fieldId: "custbody_source_po",
                    value: parentRecordId
                });
            }
        }
        /**
         * Defines the function definition that is executed before 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 beforeSubmit = (scriptContext) => {

            try {
                const currentRecord = scriptContext.newRecord;
                const parentRecordId = currentRecord.getValue({
                    fieldId: 'custbody_source_po'
                })
                const parentRecord = record.load({
                    type: record.Type.PURCHASE_ORDER,
                    id: parentRecordId,
                    isDynamic: true
                });
                let recurringNumber = parentRecord.getValue({
                    fieldId: 'custbody_recurrence_num'
                });
                recurringNumber = recurringNumber - 1;
                parentRecord.setValue({
                    fieldId: 'custbody_recurrence_num',
                    value: recurringNumber
                });
                parentRecord.save();
            } catch (error) {
                log.error("Error @beforeSubmit", error);
            }

        }
        return {beforeLoad, beforeSubmit}

    });

Note:-

• Enter only a positive integer in the Number of Recurrence field.

• Don’t enter a null value in the Number of Recurrence field.

• Don’t enter a float type value Number of Recurrence field.

• Don’t enter a string type value Number of Recurrence field.

Avatar photo

By Nandesh

I’m a Junior Software Engineer in Kerala, a Graduate with a passion for computer science, and electrical engineering.

Leave a comment

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