Suitescript 2.0 client scripts triggers on every field change

Hi. I am trying to create a client script which loads a custom form depending on the subsidiary selected.

 

The following code does that. However, now every field change is resulting in the form reloading

Why is it doing this? How do I stop this so that it only works on a change in the subsidiary field?

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/runtime", "N/url", "N/log"], function (runtime, url, log) {
  /**
   * @param {ClientScriptContext.fieldChanged} context

   */

  //test
  function fieldChanged(context) {
    var scriptObj = runtime.getCurrentScript();
    var param = scriptObj.getParameter("custscript_formid_1");
    var subParam = scriptObj.getParameter("custscript_subsidiary_1");
    var defaultForm = scriptObj.getParameter("custscript_default_form");

    var currentRecord = context.currentRecord;
    var subsidiary = context.currentRecord.getValue("subsidiary");
    // if (context.fieldId == "subsidiary") {
    //   if (subsidiary == subParam) {
    //     currentRecord.setValue({
    //       fieldId: "customform",
    //       value: param,
    //     });
    //   } else {
    //     currentRecord.setValue({
    //       fieldId: "customform",
    //       value: defaultForm,
    //     });
    //   }
    // }

    var currentUrl = document.location;
    var url = new URL(currentUrl);

    if (context.fieldId == "subsidiary") {
    }

    if (subsidiary == subParam)
      url.searchParams.set("cf", param),
        url.searchParams.set("subsidiary", subParam),
        (window.onbeforeunload = null),
        window.location.assign(url);
    else
      url.searchParams.set("cf", defaultForm),
        url.searchParams.set("subsidiary", subsidiary),
        (window.onbeforeunload = null),
        window.location.assign(url);
  }

  return {
    fieldChanged: fieldChanged,
  };
});
Rookie Asked on November 11, 2021 in SuiteScript.
Add Comment
1 Answer(s)

Actually, I realised the issue as soon as I posted this:

 

Lines 38-39

    if (context.fieldId == "subsidiary") {
    }

The closing bracket for this function needs to be after line 51

Rookie Answered on November 11, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor