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

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

To be honest, it's not really a question of "why not use onChange catalog client script instead of onSubmit": GlideAjax with onSubmit Client Scripts is just NOT compatible.  By the time the Ajax response comes back, the script has more than likely moved on and completed so it cannot respond to that response.

 

And there are a couple of other tweaks I'd do:

  1. no need to "getValue" of the field, we already have it in "newValue" (TNT: No Need to Use g_form.getValue() in onChange Scripts)
  2. I would clear the field message when the newValue == "" as well, to keep things clean
  3. Not to mention reversing the logic to make sure the value IS unique. I hate working with negatives, as they are confusing (at least to me).  It does not make sense to me that a "true" value would trigger an error message.  A "truthy" value should be a good thing.