Popup using EXTJS in client script

/**

 * @NApiVersion 2.x

 * @NScriptType ClientScript

 */

define([‘N/currentRecord’, ‘N/ui/dialog’, ‘N/search’, ‘N/url’, ‘N/record’], function (currentRecord, dialog, search, url, record) {

  function saveRecord(context) {

    try {

      let currentRecordObj = currentRecord.get();

      if (currentRecordObj.getValue({ fieldId: “custrecord14” })) {

        return true

      } else {

        loadExtJS(function () {

          showPasswordPopup(function (isValid) {

            if (isValid) {

              console.log(isValid);

              let currentRec = currentRecordObj.setValue({

                fieldId: “custrecord14”, value: “done”,

                ignoreFieldChange: true,

                forceSyncSourcing: true

              })

              // Trigger the save button click using jQuery

              triggerSaveButtonClick();

            } else {

              // Prevent record save

              let currentRec = currentRecordObj.setValue({

                fieldId: “custrecord14”, value: “”,

                ignoreFieldChange: true,

                forceSyncSourcing: true

              })

              return false;

            }

          });

        });

      }

    } catch (error) {

      console.error(‘Failed to validate password:’, error);

      return false; // Prevent save if there is an error

    }

    return false; // Prevent record save until callback is executed

  }

  function triggerSaveButtonClick() {

    // Assuming the save button has the ID ‘saveButton’

    $(‘#btn_multibutton_submitter’).click();

  }

  function loadExtJS(callback) {

    // Check if Ext JS is already loaded

    if (typeof Ext !== ‘undefined’) {

      callback();

      return;

    }

    // Create script element to load Ext JS

    var script = document.createElement(‘script’);

    script.src = ‘https://cdn.sencha.com/ext/gpl/7.3.1/build/ext-all.js’;

    script.onload = () => callback();

    script.onerror = () => {

      console.error(‘Failed to load Ext JS’);

    };

    document.head.appendChild(script);

  }

  function showPasswordPopup(callback) {

    Ext.onReady(function () {

      var passwordWindow = Ext.create(‘Ext.window.Window’, {

        title: ‘Password Validation’,

        height: 200,

        width: 400,

        layout: ‘fit’,

        modal: true,

        cls: ‘ns-window’, // Apply NetSuite window class

        items: {

          xtype: ‘form’,

          bodyPadding: 10,

          items: [

            {

              xtype: ‘textfield’,

              inputType: ‘password’,

              fieldLabel: ‘Password’,

              name: ‘password’,

              allowBlank: false,

              style: {

                fontWeight: ‘bold’, // Make the text bold

                fontSize: ’14px’, // Set the font size

                color: ‘black’, // Set the text color

                borderColor: ‘red’, // Set the border color

                padding: ‘5px’ // Add padding

              }

            },

            {

              xtype: ‘label’,

              name: ‘validationMessage’,

              text: ”,

              style: {

                fontWeight: ‘bold’,

                fontSize: ’14px’,

                color: ‘red’,

                display: ‘none’ // Initially hide the validation message

              },

              margin: ’10 0 0 0′

            }

          ],

          buttons: [

            {

              text: ‘Validate’,

              formBind: true,

              handler: function (button) {

                var form = button.up(‘form’).getForm();

                var validationMessage = form.owner.down(‘[name=validationMessage]’);

                if (form.isValid()) {

                  var password = form.findField(‘password’).getValue();

                  validatePassword(password, function (isValid) {

                    if (isValid) {

                      validationMessage.setText(‘Password accepted. The record will be saved.’, false);

                      validationMessage.setStyle({

                        color: ‘green’,

                        display: ‘block’ // Show the validation message

                      });

                      // Allow a short delay to show the message before closing the window and saving the record

                      setTimeout(function () {

                        passwordWindow.close();

                        callback(true); // Callback with true to allow save

                      }, 1500);

                    } else {

                      validationMessage.setText(‘Password rejected. The record will not be saved.’, false);

                      validationMessage.setStyle({

                        color: ‘red’,

                        display: ‘block’ // Show the validation message

                      });

                      callback(false); // Callback with false to prevent save

                    }

                  });

                }

              }

            }

          ]

        }

      });

      passwordWindow.show();

    });

  }

  function validatePassword(password, callback) {

    var isValid = true;

    callback(isValid);

  }

  return {

    saveRecord: saveRecord

  };

});

Leave a comment

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