Need compare the value entered in variable, is existed within Building [u_buidling] table

Bindhu1
Tera Contributor

In the ServiceNow catalog item, there is a variable:

1. What's the name of the new data centre? (what_s_the_name_of_the_new_data_centre) – which is of type String.

I need to compare the value entered in this variable against the Name field in the Building table (u_building). If a duplicate is found, the form should show an error on submit:
"There is already a building record with this name, please adjust."
Otherwise, the submission should be allowed.

The current script works when a duplicate name exists in the Building table, but even when a unique name is entered, it still blocks submission. Kindly help correct this behavior.
onSubmit client script[ need script in onSubmit]:

function onSubmit() {
    var dcName = g_form.getValue('what_s_the_name_of_the_new_data_centre');

    var ga = new GlideAjax('CheckBuildingName');
    ga.addParam('sysparm_name', 'checkDuplicate');
    ga.addParam('sysparm_dc_name', dcName);


    ga.getXMLAnswer(function(response) {
		alert(""+ response);
        if (response == 'true') {
			
        g_form.addErrorMessage("There is already a building record with this name, please adjust.");
       return false; // block submission
        } else {
            alert("test1");
            //g_form.submit();
			return true;
        }
    });

    return false;
}

script include:

var CheckBuildingName = Class.create();
CheckBuildingName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    checkDuplicate: function() {
        var dcName = this.getParameter('sysparm_dc_name');
        var gr = new GlideRecord('u_building');
        gr.addQuery('name', dcName);
        gr.addQuery('active', true);
        gr.query();
        return gr.hasNext() ? 'true' : 'false';
    }
});



1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Bindhu1 

why not use onChange catalog client script instead of onSubmit for this?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var dcName = g_form.getValue('what_s_the_name_of_the_new_data_centre');

    // clear field message
    g_form.hideFieldMsg('what_s_the_name_of_the_new_data_centre');

    var ga = new GlideAjax('CheckBuildingName');
    ga.addParam('sysparm_name', 'checkDuplicate');
    ga.addParam('sysparm_dc_name', dcName);
    ga.getXMLAnswer(function(response) {
        alert("" + response);
        if (response == 'true') {
            // show field message
            g_form.showFieldMsg('what_s_the_name_of_the_new_data_centre', 'There is already a building record with this name', 'error', true);
        }
    });

}

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

View solution in original post

6 REPLIES 6

Rafael Batistot
Tera Sage

Hi @Bindhu1 

 

Right now, your client script always returns false at the end of onSubmit, which blocks the submission no matter what the AJAX call returns. The asynchronous callback (getXMLAnswer) is never able to override that behavior in time.

 

To fix this, you need to use g_form.submit() inside the callback when the name is unique, and prevent the form only when a duplicate is found.

 

Follow a new script version 

 

Client script 

function onSubmit() {
var dcName = g_form.getValue('what_s_the_name_of_the_new_data_centre');
if (!dcName) {
return true; // allow submit if empty (optional)
}

var ga = new GlideAjax('CheckBuildingName');
ga.addParam('sysparm_name',

'checkDuplicate');
ga.addParam('sysparm_dc_name', dcName);

ga.getXMLAnswer(function(response) {
if (response == 'true') {
g_form.addErrorMessage("There is already a building record with this name, please adjust.");
// Do NOT submit, block here
} else {
// Allow submission manually
g_form.submit();
}
});

return false; // stop normal submit, wait for callback decision
}

 

Script include 

 

var CheckBuildingName = Class.create();
CheckBuildingName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkDuplicate: function() {
var dcName = this.getParameter('sysparm_dc_name') + '';
var gr = new GlideRecord('u_building');
gr.addQuery('name', dcName);
gr.addQuery('active', true);
gr.query();
return gr.hasNext() ? 'true' : 'false';
}
});

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Bindhu1 

you can show error message on that variable when validation fails and clear it again the next time onsubmit runs

try this

function onSubmit() {
    var dcName = g_form.getValue('what_s_the_name_of_the_new_data_centre');

	// clear field message
    g_form.hideFieldMsg('what_s_the_name_of_the_new_data_centre');

    var ga = new GlideAjax('CheckBuildingName');
    ga.addParam('sysparm_name', 'checkDuplicate');
    ga.addParam('sysparm_dc_name', dcName);


    ga.getXMLAnswer(function(response) {
        alert("" + response);
        if (response == 'true') {
			// show field message
            g_form.showFieldMsg('what_s_the_name_of_the_new_data_centre', 'There is already a building record with this name', 'error', true);
            return false; // block submission
        } else {
            alert("test1");
            //g_form.submit();
            return true;
        }
    });

    return false;
}

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

Tried with this , it still the same , its not allowing to submit when unique value entered 
onchange is not appropriate here because data center value get added up one by one like below.

Bindhu1_0-1757430274593.png

 

@Bindhu1 

this means this line is not working

g_form.submit();

try this line instead of above

g_form.orderNow();

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