Verifying if a value exists

ceraulo
Mega Guru

Hello!

I have a text field which the requester populates with a new hardware name. 
Is there a way to check if this value exists against the Hardware table and show a message that the hardware name exists?

Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

Sample script

Script Include:

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkRecordPresent: function(){

        var name = this.getParameter('sysparm_hardwareName');            
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('display_name', name);
        gr.query();
        if(gr.next()){
            return 'found';
        }
        return 'not found';
    },
    
    type: 'checkRecords'
});

Client Script:

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

	g_form.hideFieldMsg('fieldName'); // give here field/variable name

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_hardwareName', g_form.getValue('fieldName')); // give here field/variable name
	ga.getXMLAnswer(function(answer){
		if(answer == 'not found'){
			var message = 'This hardware is not present';
			g_form.clearValue('fieldName'); // give here field/variable name
			g_form.showFieldMsg('fieldName',message,'error', true);
		}
	});
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Why not have it as reference field/variable referring to alm_hardware table?

That would avoid this validation

If you still require then you would require onChange Client Script on that field and perform GlideAjax with Script Include to check if this hardware name exists in alm_hardware table

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

Sample script

Script Include:

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkRecordPresent: function(){

        var name = this.getParameter('sysparm_hardwareName');            
        var gr = new GlideRecord('alm_hardware');
        gr.addQuery('display_name', name);
        gr.query();
        if(gr.next()){
            return 'found';
        }
        return 'not found';
    },
    
    type: 'checkRecords'
});

Client Script:

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

	g_form.hideFieldMsg('fieldName'); // give here field/variable name

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_hardwareName', g_form.getValue('fieldName')); // give here field/variable name
	ga.getXMLAnswer(function(answer){
		if(answer == 'not found'){
			var message = 'This hardware is not present';
			g_form.clearValue('fieldName'); // give here field/variable name
			g_form.showFieldMsg('fieldName',message,'error', true);
		}
	});
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you!

Glad to help.

Please remember to mark response helpful as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader