We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Field Validation variable

radt
Kilo Guru

Hi, team. I need to validate a variable field. I tried variable validation regex but it serves only 80% of my solution. I have a field : number_Of_case variable. I have hr_case table. If any one enters value in number_of_case then it should validate this with table: hr_case and see whether the case number is in the table and then only allow to submit other wise this variable should throw error. I suppose a client script for on change might work but not sure how to do that.. Just not good with coding as I am new to this. 

any help?

 

 

TIA 🙂

 

1 ACCEPTED SOLUTION

Thank you for the help!! Your code helped a lot.. The exact final code used is: 

 

client script: 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var tab = new GlideAjax("sn_hr_sp.sn_hr_core_case");
tab.addParam("sysparm_name", "getCasedetails");
tab.addParam("sysparm_name_of_case", g_form.getValue('name_of_case'));
tab.getXML(ajaxResponse);

function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");

if (answer == 'false') {
g_form.setValue('name_of_case', '');
g_form.showFieldMsg("name_of_case", "The case number doesnot exist", "error");

}
}


//Type appropriate comment here, and begin script below

}

 

Script Include: sn_hr_core_case

 

var sn_hr_core_case = Class.create();

sn_hr_core_case.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getCasedetails: function(){

var newtab = new GlideRecord('sn_hr_core_case');
newtab.addQuery('number', this.getParameter('sysparm_name_of_case'));
newtab.query();
return newtab.hasNext();
},


type: 'sn_hr_core_case'
});

View solution in original post

7 REPLIES 7

Hi,

Better script

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

	getCasedetails: function(){
		var newtab = new GlideRecord('sn_hr_core_case');
		newtab.addQuery('number', this.getParameter('sysparm_name_of_case'));
		newtab.query();
		return newtab.hasNext();
	},

	type: 'Hr_case'

});

Some corrections in client script

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

	var tab = new GlideAjax("Hr_case");
	tab.addParam("sysparm_name", "getCasedetails");
	tab.addParam("sysparm_name_of_case", g_form.getValue('name_of_case'));
	tab.getXML(ajaxResponse);
	function ajaxResponse(serverResponse) {
		var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
		if(answer.toString() == 'false')
			alert('The case number doesnot exist');
	}

}

Regards
Ankur

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

Thank you for the help!! Your code helped a lot.. The exact final code used is: 

 

client script: 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var tab = new GlideAjax("sn_hr_sp.sn_hr_core_case");
tab.addParam("sysparm_name", "getCasedetails");
tab.addParam("sysparm_name_of_case", g_form.getValue('name_of_case'));
tab.getXML(ajaxResponse);

function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");

if (answer == 'false') {
g_form.setValue('name_of_case', '');
g_form.showFieldMsg("name_of_case", "The case number doesnot exist", "error");

}
}


//Type appropriate comment here, and begin script below

}

 

Script Include: sn_hr_core_case

 

var sn_hr_core_case = Class.create();

sn_hr_core_case.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getCasedetails: function(){

var newtab = new GlideRecord('sn_hr_core_case');
newtab.addQuery('number', this.getParameter('sysparm_name_of_case'));
newtab.query();
return newtab.hasNext();
},


type: 'sn_hr_core_case'
});

@radt 

Did you mistakenly marked your own response as correct?

Would you mind marking my response as correct as my code helped you.

Regards
Ankur

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