The CreatorCon Call for Content is officially open! Get started here.

Repeated Form Submission in Catalog Client Script Using getXML() in onSubmit

HarikaG62433194
Tera Contributor

I'm working on a catalog client script where I need to perform a server-side validation during the onSubmit event. The use case is to restrict form submission if the user already has a device listed in the Asset table.
I'm using getXML() to make a GlideAjax call inside the onSubmit client script. The callback function checks the response and conditionally allows or blocks the submission using return false. The validation logic works fine when the condition is met.
However, when the condition fails (i.e., the user should be allowed to submit), the form seems to be submitted multiple times, causing looping behavior. I added an alert() in the else block to debug, and it keeps triggering repeatedly

catalog client script:

function onSubmit() {
    var reason = g_form.getValue('reason_for_the_request');
    var userType = g_form.getValue('request_type');
    var deviceFor = g_form.getValue('requested_for');
    if ((reason === 'transfer' || reason === 'additional') && userType === 'individual') {
        var callScript = new GlideAjax('CheckDevice');
        callScript.addParam('sysparm_name', 'getDevice');
        callScript.addParam('sysparm_id', deviceFor);
        callScript.getXML(ajaxResponse);
        return false; // Prevent form submission until callback completes
    }
    return true; // Allow form submission if condition not met
}
function ajaxResponse(response) {
    var userData = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    var userName = userData.userDetails;
    var devicesCount = userData.devicesCount;
    var deviceName = userData.deviceName;
    var deviceSerial = userData.deviceSerial;
    if (devicesCount !== 0) {
        alert("The user " + userName + " already has " + deviceName + " with serial " + deviceSerial);
       // return false;
    } else {
       // alert("Hi");
       g_form.submit(); // Submit the form only if no device is assigned
    }
}
1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@HarikaG62433194 

check this link and enhance your script

How To: Async GlideAjax in an onSubmit script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader