'Suitelet to trigger a client script and redirect back to the Suitelet on save

EDIT 1: The code client has been edited to include the resolve url suggestion

This is a follow on from Trigger a client script on a button clicked on a Suitelet?

That was answered and now I am getting stuck with another part of the script. I have posted this on the Netsuite Community too though the response I have received there is making me more confused.

Apologies in advance if I have the StackOverflow posting etiquette/rules wrong in terms of follow up posting or cross posting.

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.

Suitelet

The custom record

Custom record

Everything is working as expected until it gets to the function saveRecord part of the client script.

I am trying to get the page to redirect back to the suitelet after the record is saved though cannot get this to work.

So far, I have tried adding in the N/redirect module into the client script and used the following :

    function saveRecord(context) {
        redirect.redirect({
        url: '/app/site/hosting/scriptlet.nl?script=313&deploy=1',
       
    });
    
        return true;
      }

The suitelet is available without login.

I have also tried

     function saveRecord(context) {
        window.open(
          "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f"
        );
    
        return true;
      }

The record is definitely saving though its not redirecting back to the Suitelet.

EDIT This is the Suitelet script in full that works:

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */
define(["N/ui/serverWidget", "N/search"], function (ui, search) {
    function getCiSetup() {
        var customrecord_nsts_ci_setupSearchObj = search.create({
            type: "customrecord_nsts_ci_setup",
            filters: [],
            columns: [
                search.createColumn({
                    name: "custrecord_nsts_ci_admin_email",
                    label: "Administrator Emails",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_search_dtl",
                    label: "Invoice Details Saved Search",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_search",
                    label: "Invoice Summary Saved Search",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_type",
                    label: "Invoice Type",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_layout",
                    label: "Default CI Layout",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_duedate",
                    label: "Due Date",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_email_sender",
                    label: "Email Sender",
                }),
                search.createColumn({
                    name: "custrecord_nsts_ci_email_template",
                    label: "Email Template",
                }),
            ],
        })

        return customrecord_nsts_ci_setupSearchObj
    }

    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 = 8378

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

            form.addButton({
                id: "other",
                label: "Other",
                functionName: "executeOther",
            })

            var sublist = form.addSublist({
                id: "custpage_ci_setup",
                type: ui.SublistType.LIST,
                label: "Consolidated Invoice Set Up",
            })

            sublist.addField({
                id: "custpage_invoice_dtl",
                label: "Invoice Details",
                type: ui.FieldType.TEXT,
            })

            sublist.addField({
                id: "custpage_invoice_summry",
                label: "Invoice Summary",
                type: ui.FieldType.TEXT,
            })
            var htmlBody = ""
            htmlBody += "<html>"
            htmlBody += "<head>"
            htmlBody += "</head>"
            htmlBody += "<body>"

            htmlBody += ""

            htmlBody += "</body></html>"

            form.addField({
                id: "custpage_html",
                label: "html",
                type: ui.FieldType.INLINEHTML,
            }).defaultValue = htmlBody
            var ciSetup = getCiSetup()
            var i = 0

            ciSetup.run().each(function (result) {
                log.debug("result", result)
                var invoiceSummary = result.getText("custrecord_nsts_ci_search")
                var invoiceDetail = result.getText("custrecord_nsts_ci_search_dtl")
                sublist.setSublistValue({
                    id: "custpage_invoice_dtl",
                    line: i,
                    value: invoiceDetail,
                })
                sublist.setSublistValue({
                    id: "custpage_invoice_summry",
                    line: i,
                    value: invoiceSummary,
                })
                i++
                return true
            })

            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 in full that works with the above Suitelet

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

  runtime,
  url,
  log
) {
  /**
   * @param {ClientScriptContext.pageInit} context
   */

  function pageInit(context) {}

  function executeRecurring(context) {
    var scriptObj = runtime.getCurrentScript();
    var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
    //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 =
      "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=436&id=1&e=T";

    var url = new URL(pageUrl);
    //Add additional code
    window.location.href = url;

    var objRecord = record.load({
      type: "customrecord_nsts_ci_setup",
      id: 1,
      isDynamic: true,
    });
    //INVOICE SUMMARY SAVED SEARCH *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search",
      value: 2079,
    });
    //INVOICE DETAILS SAVED SEARCH
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search_dtl",
      value: 2078,
    });
    //DEFAULT CI LAYOUT *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_layout",
      value: 1,
    });

    objRecord.save({
      enableSourcing: true,
      ignoreMandatoryFields: false,
    });

    // window.open(redirectUrl);
    // window.location.assign(
    //   "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f"
    // );
    //the window location is redirecting back to the suitelet screen
    window.location.assign(
      "https://tstdrv.app.netsuite.com/app/site/hosting/scriptlet.nl?script=1123&deploy=1&compid=TSTDRV&whence="
    );

    //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: "customrecord_nsts_ci_setup",
      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: "customscript_ci_suitelet",
  //   deploymentId: "customdeploy_ci_suitelet",
  // });

  // window.open(suiteletURL, "_self", false);
  // };

  // function saveRecord(context) {
  //   window.open(
  //     "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f"
  //   );

  function executeOther(context) {
    var scriptObj = runtime.getCurrentScript();
    var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
    //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 =
      "https://tstdrv0.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=436&id=1&e=T";

    var url = new URL(pageUrl);
    //Add additional code
    window.location.href = url;

    var objRecord = record.load({
      type: "customrecord_nsts_ci_setup",
      id: 1,
      isDynamic: true,
    });
    //INVOICE SUMMARY SAVED SEARCH *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search",
      value: 1669,
    });
    //INVOICE DETAILS SAVED SEARCH
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search_dtl",
      value: 1668,
    });
    //DEFAULT CI LAYOUT *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_layout",
      value: 1,
    });

    objRecord.save({
      enableSourcing: true,
      ignoreMandatoryFields: false,
    });

    // window.open(redirectUrl);
    // window.location.assign(
    //   "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f"
    // );

    window.location.assign(
      "https://tstdrv.app.netsuite.com/app/site/hosting/scriptlet.nl?script=1123&deploy=1&compid=TSTDRV&whence="
    );

    //look into window.close for the newly opened window
  }

  var customSubmit = function returnFields(context) {
    record.submitFields.promise({
      type: "customrecord_nsts_ci_setup",
      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: "customscript_ci_suitelet",
  //   deploymentId: "customdeploy_ci_suitelet",
  // });

  // window.open(suiteletURL, "_self", false);
  // };

  // function saveRecord(context) {
  //   window.open(
  //     "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f"
  //   );

  return {
    pageInit: pageInit,
    executeRecurring: executeRecurring,
    executeOther: executeOther,
    customSubmit: customSubmit,

    // saveRecord: saveRecord,
  };
});


New Version of Suitelet



Solution 1:[1]

Try this -

var suiteletURL = "https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=f";
window.open(suiteletURL, '_self', false);

If this doesn't work, you can also use url.resolveScript API under N/url module in your ClientScript.

var suiteletURL = url.resolveScript({
   scriptId: 'custom_script',
   deploymentId: 'custom_script_deployment'
});

window.open(suiteletURL, '_self', false);

Also, instead of adding this to SaveRecord in ClientScript, you can add this towards the end of your executeRecurring function.

Give this a try. Let me know in comments below.

EDIT -

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

  runtime,
  url,
  log
) {
  /**
   * @param {ClientScriptContext.pageInit} context
   */

  function pageInit(context) {}

  function executeRecurring(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);
    //Add additional code
    window.location.href = url;

    var objRecord = record.load({
      type: "customrecord_nsts_ci_setup",
      id: 1,
      isDynamic: true,
    });
    //INVOICE SUMMARY SAVED SEARCH *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search",
      value: 456,
    });
    //INVOICE DETAILS SAVED SEARCH
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_search_dtl",
      value: 452,
    });
    //DEFAULT CI LAYOUT *
    objRecord.setValue({
      fieldId: "custrecord_nsts_ci_layout",
      value: 1,
    });

    objRecord.save({
      enableSourcing: true,
      ignoreMandatoryFields: false,
    });

    window.onbeforeunload = null;

    var suiteletURL = url.resolveScript({
      scriptId: "customscript_ci_suitelet",
      deploymentId: "customdeploy_ci_suitelet",
    });

    window.open(suiteletURL, "_self", false);
  }

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1