title: Suitelet to trigger a client script and redirect back to the Suitelet on save
body: <p>EDIT 1: The code client has been edited to include the resolve url suggestion</p>
  <p>This is a follow on from <a href="https://stackoverflow.com/questions/70729005/trigger-a-client-script-on-a-button-clicked-on-a-suitelet">Trigger a client script on a button clicked on a Suitelet?</a></p>
  <p>That was answered and now I am getting stuck with another part of the script.
  I have posted this on the <a href="https://community.oracle.com/netsuite/discussion/comment/16787161#Comment_16787161" rel="nofollow noreferrer">Netsuite Community</a> too though the response I have received there is making me more confused.</p>
  <p>Apologies in advance if I have the StackOverflow posting etiquette/rules wrong in terms of follow up posting or cross posting.</p>
  <p>Scenario:
  There is a Suitelet with a button which results in a custom record page being loaded in edit mode.
  Field values are set on the custom record using the client script that is triggered when the button on the suitelet is clicked.</p>
  <p><a href="https://i.stack.imgur.com/PdOwL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PdOwL.png" alt="Suitelet" /></a></p>
  <p>The custom record</p>
  <p><a href="https://i.stack.imgur.com/dXj0o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dXj0o.png" alt="Custom record" /></a></p>
  <p>Everything is working as expected until it gets to the function saveRecord part of the client script.</p>
  <p>I am trying to get the page to redirect back to the suitelet after the record is saved though cannot get this to work.</p>
  <p>So far, I have tried adding in the N/redirect module into the client script and used the following :</p>
  <pre class="lang-js prettyprint-override"><code>    function saveRecord(context) {
          redirect.redirect({
          url: '/app/site/hosting/scriptlet.nl?script=313&amp;deploy=1',
         
      });
      
          return true;
        }
  </code></pre>
  <p>The suitelet is available without login.</p>
  <p>I have also tried</p>
  <pre class="lang-js prettyprint-override"><code>     function saveRecord(context) {
          window.open(
            &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&amp;id=1&amp;e=f&quot;
          );
      
          return true;
        }
  </code></pre>
  <p>The record is definitely saving though its not redirecting back to the Suitelet.</p>
  <p>EDIT
  This is the Suitelet script in full that works:</p>
  <pre class="lang-js prettyprint-override"><code>/**
   *@NApiVersion 2.x
   *@NScriptType Suitelet
   */
  define([&quot;N/ui/serverWidget&quot;, &quot;N/search&quot;], function (ui, search) {
      function getCiSetup() {
          var customrecord_nsts_ci_setupSearchObj = search.create({
              type: &quot;customrecord_nsts_ci_setup&quot;,
              filters: [],
              columns: [
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_admin_email&quot;,
                      label: &quot;Administrator Emails&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_search_dtl&quot;,
                      label: &quot;Invoice Details Saved Search&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_search&quot;,
                      label: &quot;Invoice Summary Saved Search&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_type&quot;,
                      label: &quot;Invoice Type&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_layout&quot;,
                      label: &quot;Default CI Layout&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_duedate&quot;,
                      label: &quot;Due Date&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_email_sender&quot;,
                      label: &quot;Email Sender&quot;,
                  }),
                  search.createColumn({
                      name: &quot;custrecord_nsts_ci_email_template&quot;,
                      label: &quot;Email Template&quot;,
                  }),
              ],
          })
  
          return customrecord_nsts_ci_setupSearchObj
      }
  
      var exports = {}
  
      function onRequest(context) {
          if (context.request.method === &quot;GET&quot;) {
              var form = ui.createForm({
                  title: &quot;Consolidated Invoicing Type&quot;,
              })
              // form.clientScriptModulePath =
              //   &quot;SuiteScripts/sdf_ignore/Consolidated Invoice Client Script.js&quot;;
              form.clientScriptFileId = 8378
  
              form.addButton({
                  id: &quot;recurring&quot;,
                  label: &quot;Recurring&quot;,
                  functionName: &quot;executeRecurring&quot;,
              })
  
              form.addButton({
                  id: &quot;other&quot;,
                  label: &quot;Other&quot;,
                  functionName: &quot;executeOther&quot;,
              })
  
              var sublist = form.addSublist({
                  id: &quot;custpage_ci_setup&quot;,
                  type: ui.SublistType.LIST,
                  label: &quot;Consolidated Invoice Set Up&quot;,
              })
  
              sublist.addField({
                  id: &quot;custpage_invoice_dtl&quot;,
                  label: &quot;Invoice Details&quot;,
                  type: ui.FieldType.TEXT,
              })
  
              sublist.addField({
                  id: &quot;custpage_invoice_summry&quot;,
                  label: &quot;Invoice Summary&quot;,
                  type: ui.FieldType.TEXT,
              })
              var htmlBody = &quot;&quot;
              htmlBody += &quot;&lt;html&gt;&quot;
              htmlBody += &quot;&lt;head&gt;&quot;
              htmlBody += &quot;&lt;/head&gt;&quot;
              htmlBody += &quot;&lt;body&gt;&quot;
  
              htmlBody += &quot;&quot;
  
              htmlBody += &quot;&lt;/body&gt;&lt;/html&gt;&quot;
  
              form.addField({
                  id: &quot;custpage_html&quot;,
                  label: &quot;html&quot;,
                  type: ui.FieldType.INLINEHTML,
              }).defaultValue = htmlBody
              var ciSetup = getCiSetup()
              var i = 0
  
              ciSetup.run().each(function (result) {
                  log.debug(&quot;result&quot;, result)
                  var invoiceSummary = result.getText(&quot;custrecord_nsts_ci_search&quot;)
                  var invoiceDetail = result.getText(&quot;custrecord_nsts_ci_search_dtl&quot;)
                  sublist.setSublistValue({
                      id: &quot;custpage_invoice_dtl&quot;,
                      line: i,
                      value: invoiceDetail,
                  })
                  sublist.setSublistValue({
                      id: &quot;custpage_invoice_summry&quot;,
                      line: i,
                      value: invoiceSummary,
                  })
                  i++
                  return true
              })
  
              context.response.writePage(form)
          } else if ((context.response.method = &quot;POST&quot;)) {
              log.debug({
                  title: &quot;request method type&quot;,
                  details: &quot;suitelet is posting&quot;,
              })
          }
      }
      exports.onRequest = onRequest
      return exports
  })
  
  
  </code></pre>
  <p>This is the client script in full that works with the above Suitelet</p>
  <pre class="lang-js prettyprint-override"><code>/**
   *@NApiVersion 2.x
   *@NScriptType ClientScript
   */
  define([&quot;N/record&quot;, &quot;N/runtime&quot;, &quot;N/url&quot;, &quot;N/log&quot;], function (
    record,
  
    runtime,
    url,
    log
  ) {
    /**
     * @param {ClientScriptContext.pageInit} context
     */
  
    function pageInit(context) {}
  
    function executeRecurring(context) {
      var scriptObj = runtime.getCurrentScript();
      var recordType = scriptObj.getParameter(&quot;custscript_ci_suitelet_record&quot;);
      //page url is for the link to the CI invoice set up page when it is in edit mode. There is only one record that can be saved for this record type
      var pageUrl =
        &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=436&amp;id=1&amp;e=T&quot;;
  
      var url = new URL(pageUrl);
      //Add additional code
      window.location.href = url;
  
      var objRecord = record.load({
        type: &quot;customrecord_nsts_ci_setup&quot;,
        id: 1,
        isDynamic: true,
      });
      //INVOICE SUMMARY SAVED SEARCH *
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_search&quot;,
        value: 2079,
      });
      //INVOICE DETAILS SAVED SEARCH
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_search_dtl&quot;,
        value: 2078,
      });
      //DEFAULT CI LAYOUT *
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_layout&quot;,
        value: 1,
      });
  
      objRecord.save({
        enableSourcing: true,
        ignoreMandatoryFields: false,
      });
  
      // window.open(redirectUrl);
      // window.location.assign(
      //   &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&amp;id=1&amp;e=f&quot;
      // );
      //the window location is redirecting back to the suitelet screen
      window.location.assign(
        &quot;https://tstdrv.app.netsuite.com/app/site/hosting/scriptlet.nl?script=1123&amp;deploy=1&amp;compid=TSTDRV&amp;whence=&quot;
      );
  
      //look into window.close for the newly opened window
    }
    //this is reiterating the parameters for the saved searches that was entered above
    var customSubmit = function returnFields(context) {
      record.submitFields.promise({
        type: &quot;customrecord_nsts_ci_setup&quot;,
        id: 1,
        values: {
          custrecord_nsts_ci_layout: 1,
          custrecord_nsts_ci_search_dtl: 2078,
          custrecord_nsts_ci_search: 2079,
        },
      });
    };
    // window.onbeforeunload = null;
  
    // var suiteletURL = url.resolveScript({
    //   scriptId: &quot;customscript_ci_suitelet&quot;,
    //   deploymentId: &quot;customdeploy_ci_suitelet&quot;,
    // });
  
    // window.open(suiteletURL, &quot;_self&quot;, false);
    // };
  
    // function saveRecord(context) {
    //   window.open(
    //     &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&amp;id=1&amp;e=f&quot;
    //   );
  
    function executeOther(context) {
      var scriptObj = runtime.getCurrentScript();
      var recordType = scriptObj.getParameter(&quot;custscript_ci_suitelet_record&quot;);
      //on the  record for the client script, there is a parameter for    List/Record pointing to record type: CI - Setup
      //the client script is deployed against the suitelet record
      //on the deployment record, there is a parameter pointing to record ID 1 which is the only set up record that can exist since there cannot be more than one set up record
  
      //edit mode for the CI invoice set up page record
      var pageUrl =
        &quot;https://tstdrv0.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=436&amp;id=1&amp;e=T&quot;;
  
      var url = new URL(pageUrl);
      //Add additional code
      window.location.href = url;
  
      var objRecord = record.load({
        type: &quot;customrecord_nsts_ci_setup&quot;,
        id: 1,
        isDynamic: true,
      });
      //INVOICE SUMMARY SAVED SEARCH *
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_search&quot;,
        value: 1669,
      });
      //INVOICE DETAILS SAVED SEARCH
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_search_dtl&quot;,
        value: 1668,
      });
      //DEFAULT CI LAYOUT *
      objRecord.setValue({
        fieldId: &quot;custrecord_nsts_ci_layout&quot;,
        value: 1,
      });
  
      objRecord.save({
        enableSourcing: true,
        ignoreMandatoryFields: false,
      });
  
      // window.open(redirectUrl);
      // window.location.assign(
      //   &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&amp;id=1&amp;e=f&quot;
      // );
  
      window.location.assign(
        &quot;https://tstdrv.app.netsuite.com/app/site/hosting/scriptlet.nl?script=1123&amp;deploy=1&amp;compid=TSTDRV&amp;whence=&quot;
      );
  
      //look into window.close for the newly opened window
    }
  
    var customSubmit = function returnFields(context) {
      record.submitFields.promise({
        type: &quot;customrecord_nsts_ci_setup&quot;,
        id: 1,
        values: {
          custrecord_nsts_ci_layout: 1,
          custrecord_nsts_ci_search_dtl: 1669,
          custrecord_nsts_ci_search: 1668,
        },
      });
    };
    // window.onbeforeunload = null;
  
    // var suiteletURL = url.resolveScript({
    //   scriptId: &quot;customscript_ci_suitelet&quot;,
    //   deploymentId: &quot;customdeploy_ci_suitelet&quot;,
    // });
  
    // window.open(suiteletURL, &quot;_self&quot;, false);
    // };
  
    // function saveRecord(context) {
    //   window.open(
    //     &quot;https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&amp;id=1&amp;e=f&quot;
    //   );
  
    return {
      pageInit: pageInit,
      executeRecurring: executeRecurring,
      executeOther: executeOther,
      customSubmit: customSubmit,
  
      // saveRecord: saveRecord,
    };
  });
  
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Gl5Eu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gl5Eu.png" alt="New Version of Suitelet" /></a></p>