Glide Record not found for hasNext

Tim T
Tera Contributor

My requirements are to query the Incident table on submit from the portal to identify any potential similar issues at the same location; alert the user they may be creating a duplicate record; and confirm they want to proceed with opening the new Incident. Using the below script in a Catalog Client Script, however, I do not get the expected confirmation window.  I even commented out the query for the custom location field and tried to pull results for any Incidents in an open status.  Still, I get no confirmation window.  Is there something I'm missing from the below code?

 

function onSubmit() {
 
    // Retrieve the values of the 'location' variables
    var location = g_form.getValue('location'); // Replace 'location' with the actual variable name
 
    // Query the incident table for matching records
    var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery('u_property_location',location); // Replace with the actual field name
        incidentGr.addQuery('state','IN','1,2,3'); // Submitted (1) or Acknowledged (2) or Work in Progress (3)
        incidentGr.query();
 
    // Check if any matching incidents exist
if (incidentGr.hasNext()) {
    
// Create a confirmation message
var confirmMessage = confirm("There is already a ticket open for reporting an issue with. Do you want to proceed with opening this ticket?");
 
// Display the confirmation pop-up
if (confirmMessage == false) {
// If the user cancels, prevent the form from submitting
                return false;
            }
}
        }

 

10 REPLIES 10

@Tim T  Can you apply some log and check where it is getting failed

I added logs below.  The location in the log is correct.  The response comes back with a null value

 

function onSubmit() {
    var location = g_form.getValue('location'); // Replace 'location' with the actual variable name
   
    if (location) {
        var ga = new GlideAjax('CheckDuplicateIncident'); // Call the Script Include
        ga.addParam('sysparm_name', 'checkDuplicate');
        ga.addParam('sysparm_location', location);
        console.log('Location is ',location);

        ga.getXMLAnswer(function(response) {
                    console.log('Response is',response);
            if (response == 'true') {
                var confirmMessage = confirm("There is already a ticket open for this location. Do you want to proceed with opening this ticket?");
                if (!confirmMessage) {
                    g_form.clearMessages();
                    g_form.addErrorMessage("Submission canceled due to potential duplicate.");
                    g_form.submit(false);
                }
            }
        });
    }
   
}

Juhi Poddar
Kilo Patron

Hello @Tim T 

Your script has a few issues that might be preventing the confirmation window from appearing. Here are the key problems and solutions:

Issues in Your Code:

  1. onSubmit() Needs to Return false to Prevent Submission
    • If you want to stop form submission, onSubmit must return false. Right now, your function only prevents submission if a duplicate is found but doesn’t return true otherwise.
  2. GlideRecord (GlideAjax is Required Instead)
    • GlideRecord does not work on the client-side. It only runs on the server.
    • You must use GlideAjax to query the incident table from a client-side script.

onSubmit Client script:

 

function onSubmit() {
    var location = g_form.getValue('location'); // Replace with actual field name

    if (!location) {
        return true; // Proceed if no location is selected
    }

    var ga = new GlideAjax('CheckDuplicateIncident');
    ga.addParam('sysparm_name', 'checkForDuplicates');
    ga.addParam('sysparm_location', location);

    var result = ga.getXMLWait(); // Synchronous call
    var duplicateExists = result.documentElement.getAttribute("answer") == "true";

    if (duplicateExists) {
        var confirmMessage = confirm("There is already an open ticket for this location. Do you want to proceed?");
        if (!confirmMessage) {
            return false; // Prevent submission
        }
    }

    return true; // Proceed with submission
}

 

 Script include:

 

var CheckDuplicateIncident = Class.create();
CheckDuplicateIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
    checkForDuplicates: function() {
        var location = this.getParameter('sysparm_location');
        var gr = new GlideRecord('incident');
        gr.addQuery('u_property_location', location);
        gr.addQuery('state', 'IN', '1,2,3'); // Open states
        gr.query();

        return gr.hasNext(); // Returns true if a duplicate exists, false otherwise
    }
});

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Juhi,

Thank you for your response and suggestion.  When I tried executing the scripts you provided, I received the below console error messages.

 

GlideAjax.getXMLWait is no longer supported

 

(g_env) [SCRIPT:EXEC] Error while running Client Script "No Duplicate Confirmation - Cameras": TypeError: Cannot read properties of undefined (reading 'documentElement')

@Tim T Yes getXMLWait() do not work on portal side instead try to update your client script something like below

function onSubmit() {
 if (g_scratchpad.isFormValid) {
        return true;
    }
    var location = g_form.getValue('location'); // Replace with actual field name

    if (!location) {
        return true; // Proceed if no location is selected
    }

    var ga = new GlideAjax('CheckDuplicateIncident');
    ga.addParam('sysparm_name', 'checkForDuplicates');
    ga.addParam('sysparm_location', location);
   return false;
    var result = ga.getXMLWait(); // Synchronous call
    var duplicateExists = result.documentElement.getAttribute("answer") == "true";

    if (duplicateExists) {
        var confirmMessage = confirm("There is already an open ticket for this location. Do you want to proceed?");
        if (!confirmMessage) {
            return false; // Prevent submission
        }
   var actionName = g_form.getActionName();
    g_scratchpad.isFormValid = true;
    g_form.submit(actionName);
    }

}

 

Please mark correct/helpful if this helps you