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

Ankur Bawiskar
Tera Patron
Tera Patron

@Tim T 

using GlideRecord in client side is not recommended.

why not have onChange catalog client script on Location and throw error if validation fails?

With this you ensure correct location is selected

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

@Tim T 

Hope you are doing good.

Did my reply answer your question?

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

sunil maddheshi
Tera Guru

@Tim T 

Don't use GlideRecord on client side, Use client callable  script include and client script.

Script Inlcude:

var CheckDuplicateIncident = Class.create();
CheckDuplicateIncident.prototype = {
    initialize: function() {},

    checkDuplicate: function(location) {
        var result = false;
        var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery('u_property_location', location); // Replace with the actual field name
        incidentGr.addQuery('state', 'IN', '1,2,3'); // Checking Open status incidents
        incidentGr.query();

        if (incidentGr.hasNext()) {
            result = true; // Duplicate found
        }

        return result;
    },

    type: 'CheckDuplicateIncident'
};

 

Client script:

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);
        
        ga.getXMLAnswer(function(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);
                }
            }
        });
    }
}

Please mark correct/helpful if this helps you!

Giga,

Thank you for your suggestion.  When I tried executing your scripts, I am not seeing the expected confirmation window though there are several Incidents currently in an open status with the same location as the one selected.  Any recommendations on what to check?