1

I am trying to update the values on a custom record based on the button clicked on a suitelet

suitelet

This will have 3 or 4 different buttons.

There is only one set up page that can be loaded on click of the button.

For example, 'recurring' is clicked on the suitelet. This will load a custom record page and set the parameters. If 'fortnightly invoice' button is clicked, this will also load the same custom record page and set the parameters.

Where I am getting stuck: I have a suitelet with the buttons that are designed to load the custom record by calling a client script function

Instead, as soon as the suitelet loads, the custom record page loads and is stuck in an infinite loop reloading again and again

This is my suitelet script:


    /**
     *@NApiVersion 2.x
     *@NScriptType Suitelet
     */
    define(["N/ui/serverWidget"], function (ui) {
      var exports = {};
    
      function onRequest(context) {
        if (context.request.method === "GET") {
          var form = ui.createForm({
            title: "Consolidated Invoicing Type",
          });
          // form.clientScriptModulePath =
          //   "SuiteScripts/sdf_ignore/Consolidated Invoice Client Script.js";
          form.clientScriptFileId = 2659;
    
          form.addButton({
            id: "recurring",
            label: "Recurring",
            functionName: "pageInit",
          });
    
          context.response.writePage(form);
        } else if ((context.response.method = "POST")) {
          log.debug({
            title: "request method type",
            details: "suitelet is posting",
          });
        }
      }
      exports.onRequest = onRequest;
      return exports;
    });

This is the client script:

    /**
     *@NApiVersion 2.x
     *@NScriptType ClientScript
     */
    define(["N/record", "N/runtime", "N/url"], function (
      record,
    
      runtime,
      url
    ) {
      /**
       * @param {ClientScriptContext.pageInit} context
       */
      function pageInit(context) {
        var scriptObj = runtime.getCurrentScript();
        var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
        var pageUrl =
          "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=T";
    
        var url = new URL(pageUrl);

        window.location.href = url;
      }
    
      return {
        pageInit: pageInit,
      };
    });

Do I need to use another script type to set the values on the custom record? (i.e not a client script)

How would I link a userevent script to the suitelet so that it is triggered on button click?

Why would the client script be automatically initiated on loading the suitelet page if it is supposed to be tied to a button on the form?

Thanks

Vernita
  • 93
  • 3
  • 15

1 Answers1

2

The page keeps reloading because the "pageInit" function in your client script will be executed automatically by Netsuite, because "pageInit" is a default Netsuite Entry function. If you just rename your function it will work:

On the Suitelet:

form.addButton({
        id: "recurring",
        label: "Recurring",
        functionName: "executeRecurring",
      });

On the client script:

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/record", "N/runtime", "N/url"], function (
  record,

  runtime,
  url
) {
  /**
   * @param {ClientScriptContext.pageInit} context
   */
  function executeRecurring() {
    var scriptObj = runtime.getCurrentScript();
    var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
    var pageUrl =
      "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=T";

    var url = new URL(pageUrl);

    window.location.href = url;
  }

  function pageInit(context) { // you need to keep at least one Netsuite Entry function, otherwise you will get an error
  }

  return {
    pageInit: pageInit,
    executeRecurring: executeRecurring
  };
});

Also if the parameter custscript_ci_suitelet_record is a parameter on the Suitelet script, then you won't be able to get its value on the client script, you must get the value on the Suitelet script and pass it as parameter during the button call:

Suitelet:

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
define(["N/ui/serverWidget", "N/runtime"], function (ui, runtime) {
  var exports = {};

  function onRequest(context) {
    if (context.request.method === "GET") {
      var form = ui.createForm({
        title: "Consolidated Invoicing Type",
      });
      // form.clientScriptModulePath =
      //   "SuiteScripts/sdf_ignore/Consolidated Invoice Client Script.js";
      form.clientScriptFileId = 2659;
      
    var recordType = runtime.getCurrentScript().getParameter("custscript_ci_suitelet_record");

      form.addButton({
        id: "recurring",
        label: "Recurring",
        functionName: "executeRecurring('" + recordType + "')",
      });

      context.response.writePage(form);
    } else if ((context.response.method = "POST")) {
      log.debug({
        title: "request method type",
        details: "suitelet is posting",
      });
    }
  }
  exports.onRequest = onRequest;
  return exports;
});

Client script:

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/record", "N/runtime", "N/url"], function (
  record,

  runtime,
  url
) {
  /**
   * @param {ClientScriptContext.pageInit} context
   */
  function executeRecurring(recType) {
    var pageUrl =
      "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=" + recType + "&id=1&e=T";

    var url = new URL(pageUrl);

    window.location.href = url;
  }

  function pageInit(context) { // you need to keep at least one Netsuite Entry function, otherwise you will get an error
  }

  return {
    pageInit: pageInit,
    executeRecurring: executeRecurring
  };
});
B. Assem
  • 1,058
  • 9
  • 12
  • Thank you so much B.Assem! You explained this really well and the solution you suggested to rename the function worked perfectly – Vernita Jan 17 '22 at 06:31