Creating and Saving Virtual Field Values in NetSuite Using SuiteScript

Creating a Virtual Field: A virtual field is created in the beforeLoad function of a User Event script. This virtual field displays as a selectable list field in create and edit modes, while the original field remains hidden.

Saving the Selected Value: Before the record is submitted, the selected value from the virtual field is transferred to the original free-form text field using the beforeSubmit function. This allows the original field to store dynamically chosen values without affecting its data type.

const beforeLoad = (scriptContext) => {

      try {

        if (scriptContext.type === scriptContext.UserEventType.CREATE ||

          scriptContext.type === scriptContext.UserEventType.EDIT) {

          let newRecord = scriptContext.newRecord;

          let form = scriptContext.form;

          let turnaroundText = form.getField({

            id: ‘custrecord_jj_st_turn_around_time’

          });

          if (turnaroundText) {

            turnaroundText.updateDisplayType({

              displayType: serverWidget.FieldDisplayType.HIDDEN

            });

          }

          // Create a virtual list field on the form

          let virtualField = form.addField({

            id: ‘custpage_virtual_field’,

            type: ‘select’,

            label: ‘Turnaround Time’

          });

          form.insertField({

            field: virtualField,

            nextfield: ‘custrecord_vr_svcord_turnaround’

          });

          let turnaroundTimeValue = newRecord.getValue(‘custrecord_jj_st_turn_around_time’);

          log.debug(“turnaroundTimeText”, turnaroundTimeValue)

        }

      } catch (e) {

        log.error(“Error @ beforeLoad”, e);

      }

    }

    const beforeSubmit = (scriptContext) => {

      try {

        let newRecord = scriptContext.newRecord;

        // Get the value from the virtual list field

        let selectedVirtualFieldValue = newRecord.getValue(‘custpage_virtual_field’);

        // Set the value to the free-form text field of turn around time

        if (selectedVirtualFieldValue) {

          newRecord.setValue({

            fieldId: ‘custrecord_jj_st_turn_around_time’,

            value: selectedVirtualFieldValue

          });

        }

      }

      catch (e) {

        log.error(“Error @ beforeSubmit”, e)

      }

    }

Leave a comment

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