Onsubmit Popup Catalog Script

Ricardo Sanchez
Tera Contributor

I created an onSubmit script, but I'm running into a problem. When the user clicks "I agree," it should submit the form. However, even though the script sets the value to “true,” g_form.onsubmit is not actually triggering, and the form remains on the same page instead of submitting.

function onSubmit() {
    // Define the agreement HTML
    event.preventDefault();

    var agreementHTML =
        '<div style="text-align: left; font-size: 14px; line-height: 1.6; font-family: Arial, sans-serif;">' +
        '<h2 style="text-align: center;">Admin User Agreement</h2>' +
        '<p><strong>The admin user:</strong></p>' +
        '<ol>' +
        '<li>Accepts responsibility for using all software in accordance with applicable license terms.</li>' +
        '<li>Will retain records of license information and purchase documentation for any new applications installed. ' +
        'Copies of all records will be provided to IT upon request to support internal and external software audits, ' +
        'including audits of security and licensing compliance.</li>' +
        '<li>Agrees not to reconfigure/disable anti-virus components; modify anti-virus scan or automatic update schedules; ' +
        'or reconfigure/disable other security products installed by  IT.</li>' +
        '<li>Accepts responsibility for the security of the applications they install and configuration changes they make. ' +
        'For example, all network-connected VMs should have antivirus protection.</li>' +
        '<li>Will be prompted for justification each time they escalate permissions. Responses should be a sentence or less ' +
        'and must describe why each admin action is required to support contract requirements. These justifications will ' +
        'be audited internally and by government auditors.</li>' +
        '<li>Agrees to connect the computer to the network, either directly at an Office location or using the Advanced VPN, ' +
        'at least monthly to receive updates. The computer should remain connected for at least six (6) consecutive hours each month ' +
        'to ensure time for updates to install.</li>' +
        '<li>Will not take the computer outside the US with admin rights enabled. Please contact IT for a loaner laptop or to have ' +
        'admin rights temporarily removed for travel abroad.</li>' +
        '</ol>' +
        '<h3>Standard Software:</h3>' +
        '<ul>' +
        '<li>Microsoft Windows 10 Enterprise</li>' +
        '<li>Microsoft Office</li>' +
        '<li>Microsoft Edge</li>' +
        '<li>Firefox</li>' +
        '<li>Antivirus Software as Specified by IT</li>' +
        '<li>CrowdStrike Falcon</li>' +
        '<li>Pulse Secure Client</li>' +
        '<li>ClearPass OnGuard</li>' +
        '</ul>' +
        '</div>';

    // Display the agreement in a modal
      spModal.open({
        title: "Admin User Agreement",
        message: agreementHTML,
        buttons: [
            { label: "✔ Agree and Submit", primary: true },
            { label: "✘ Cancel", cancel: true }
        ],
        backdrop: "static",
        keyboard: false
    }).then(function(answer) {
        if (answer.label === "✔ Agree and Submit") {
            console.log("User agreed to the terms.");

            // Set the 'u_agreed_to_terms' field to true
            g_form.setValue('u_agreed_to_terms', true);

            // Programmatically trigger form submission
            g_form.submit();
        } else {
            console.log("User canceled the agreement.");
            alert("Submission canceled. You must agree to proceed.");
        }
    });

    console.log("Modal displayed. Waiting for user action.");
    return false; // Explicitly block submission
}
1 REPLY 1

Nick Parsons
Mega Sage

Your issue is that g_form.submit() will cause your onSubmit handler to get triggered again, causing your submission to get blocked again and the popup to appear again. You need to set some sort of persistant indicator that can exist across multiple calls to your onSubmit handler which indicates to your script if the user has agreed to the modal.

 

One way to do that is with scratchpad:

function onSubmit() {
    // Define the agreement HTML
    // check to see if agreedToTerms is truthy, if it is, allow regular submit
    if (g_scratchpad.agreedToTerms) { 
      return true;
    }

    var agreementHTML = ...
    // Display the agreement in a modal
    spModal.open(...).then(function(answer) {
      if (answer.label === "✔ Agree and Submit") {
        console.log("User agreed to the terms.");
        
        // Set the 'u_agreed_to_terms' field to true
        g_form.setValue('u_agreed_to_terms', true);
        // Set flag to indicate to onSubmit that terms have been agreed to
        g_scratchpad.agreedToTerms = true; 
        // Programmatically trigger form submission
        g_form.submit();
      } else {
         ...
      }
    });

    console.log("Modal displayed. Waiting for user action.");
    return false; // Explicitly block submission
}

 You could also do this without g_sratchpad if you were to instead use g_form.getBooleanValue("u_agreed_to_terms") in the top if-statement, but if this field is visible and editable to the end user then they can change it without reading the popup, so that's when g_scratchpad approach above might be better.