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.

please provide the solution for the following query

Priyapatnayak
Tera Contributor
 
Can someone please tell that what is the exact mistake in my code. when i tried to give the existing request name its able to stop the submission of form, But its also not allowing  me to submit the form even I am giving the new request name in the form.. getting the same error like "The request name must be unique" even i am mentioning the new request name in form. please help me to sort out this.
 
Script Include:
var CheckUniqueRequestNameScriptInclude = Class.create();
CheckUniqueRequestNameScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isRequestNameUnique: function(reqName) {
        var gr = new GlideRecord('sc_cat_item_producer'); // Replace with the appropriate table name.
        gr.addQuery('request_name', reqName);
        gr.query();
        if (gr.next()) {
            return 'false'; // Not unique.
        }
       return 'true'; // Unique.
    }
});
 
 
onchange catalog client script:
function onSubmit() {
   //Type appropriate comment here, and begin script below
       var requestName = g_form.getValue('request_name'); // Get the request name.

    // Use GlideAjax to check if the request name is unique.
    var ga = new GlideAjax('CheckUniqueRequestNameScriptInclude');
    ga.addParam('sysparm_name', 'isRequestNameUnique');
    ga.addParam('sysparm_requestName', requestName);
    ga.getXMLAnswer(function(response) {
        if (response === 'false') {
            // Request name is not unique; show an error message.
            g_form.addErrorMessage('The request name must be unique.');
        } else {
            // Request name is unique; allow the submission.
            g_form.submit();
        }
    });

    return false;// Return false to prevent the default form submission behavior
}
2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

GlideRecord queries have this neat(?) feature where if an addQuery line is unknown/invalid it ignores it.  Your addQuery is invalid because there is not a column named 'request_name' on the sc_cat_item_producer table, and you'll also need to define reqName.  With GlideAjax there is not an argument passed in with the function call like you can do in a Reference qualifier, Business Rule,... so you need to define the parameter that you're passing in from the Client like this:

    isRequestNameUnique: function() {
        var reqName = this.getParameter('sysparm_requestName');
        var gr = new GlideRecord('sc_cat_item_producer'); // Replace with the appropriate table name.
        gr.addQuery('name', reqName);
        gr.query();
        if (gr.next()) {
            return 'false'; // Not unique.
        }
       return 'true'; // Unique.
    }

 

Debendu
Tera Contributor

Hi @Priyapatnayak ,

As you are passing the request number using glide ajax, use this.getParameter('sysparm_requestName') in the script include to purse the request number.

Updated Script Include Code:

var CheckUniqueRequestNameScriptInclude = Class.create();
CheckUniqueRequestNameScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isRequestNameUnique: function() {
        var gr = new GlideRecord('sc_cat_item_producer'); // Replace with the appropriate table name.

        // Updated Line
        gr.addQuery('request_name', this.getParameter('sysparm_requestName'));

        gr.query();
        if (gr.next()) {
            return 'false'; // Not unique.
        }
       return 'true'; // Unique.
    }
});