Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to prevent incident form submission using ui page buttons

Akash_16
Tera Contributor

Hi All,
There is a use case on incident, Before the submission of incident form we need to show a dialog box(GlideModal), It consists of some data on it, On the dialog box(GlideModal) we have provided two buttons(accept suggestion,create incident) if he click on accept suggestion we need to prevent the record submission, If he click on create incident then we need to allow him to submit the incident. When the user saves the record before submission the dialog box(GlideModal) is  coming on the form and buttons are also coming but the functionality is not working.
GlideModal on incident form:

Akash_16_0-1761672535006.png

 


To achieve this I have created a ui page and Onsubmit client script and the data is coming from script include.
Below are the scripts from client script and ui page:

Client script (onSubmit):

function onSubmit() {
    // If we allowed it already, submit normally
    if (g_scratchpad.allowSubmit === true) {
        g_scratchpad.allowSubmit = false;
        return true;
    }

    // Run only for new records
    if (!g_form.isNewRecord()) return true;

    var ga = new GlideAjax('');
    ga.addParam('sysparm_name', '');
    ga.addParam('sysparm_data', JSON.stringify({
        short_description: g_form.getValue('short_description')
    }));

    ga.getXMLAnswer(function (response) {
        var message = response || "Would you like to accept this suggestion or create an incident?";

        // Create modal
        var dialog = new GlideModal('next_best_action_confirm', false, 500);
        dialog.setTitle('Next Best Action');
        dialog.setPreference('message', message);

        // 👇 Define behavior for Accept and Create buttons
        dialog.setPreference('onAccept', function() {
            g_form.addInfoMessage("User accepted suggestion. Record not submitted.");
            dialog.destroy(); // just close
        });

        dialog.setPreference('onCreate', function() {
            g_form.addInfoMessage("User chose to create incident.");
            dialog.destroy();
            g_scratchpad.allowSubmit = true;
            g_form.submit(); // resubmit
        });

        dialog.render();
    });

    // Stop submission until modal interaction
    return false;
}
UI PAGE:
<j:jelly xmlns:j="jelly:core" xmlns:g="glide">
  <g:ui_form onsubmit="return false;">
    <table style="width: 100%; min-width: 400px; padding: 12px;">
      <g:evaluate jelly="true">
        var message = (jelly.RP.getWindowProperties().get('message') || '') + '';
        message = new GlideStringUtil().unEscapeHTML(message);
      </g:evaluate>

      <tr>
        <td>
          <div style="white-space: pre-line; margin-bottom: 16px;">
            <g:no_escape>${message}</g:no_escape>
          </div>
        </td>
      </tr>

      <tr>
        <td align="right">
          <button id="acceptBtn" type="button" class="btn btn-primary" style="margin-right: 8px;">
            Accept Suggestion
          </button>
          <button id="createBtn" type="button" class="btn btn-default">
            Create Incident
          </button>
        </td>
      </tr>
    </table>
  </g:ui_form>

  <script>
    document.addEventListener("DOMContentLoaded", function () {
      var gdw = GlideDialogWindow.get();
      if (!gdw) return;

      var onAccept = gdw.getPreference("onAccept");
      var onCreate = gdw.getPreference("onCreate");

      // Button events
      var acceptBtn = document.getElementById("acceptBtn");
      var createBtn = document.getElementById("createBtn");

      if (acceptBtn) {
        acceptBtn.addEventListener("click", function () {
          if (typeof onAccept === "function") onAccept.call(gdw);
        });
      }

      if (createBtn) {
        createBtn.addEventListener("click", function () {
          if (typeof onCreate === "function") onCreate.call(gdw);
        });
      }
    });
  </script>
</j:jelly>
0 REPLIES 0