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

Request you to visit your older question and close by marking appropriate response correct and helpful.

Attaching a csv file to notification

Regards
Ankur

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