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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

So the number entered should be a valid HR Case. Instead of a Single Line Text with a regex, have you considered a mandatory Reference variable, where you only can select an existing HR Case?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Yeah.. we proposed it but client is particular that end user will mess with that if they have n number to article numbers come in the reference. They want user to add it specifically instead to select from list. We have to use single line text as end user will give the number which they have and client want it to have as a single line text but with validation.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

yes onChange client script + GlideAjax will be required.

What did you start and where are you stuck?

GlideAjax Example Cheat Sheet

Regards
Ankur

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

client script

function onChange(control, oldValue, newValue, isLoading) {


if (isLoading || newValue == '') {


return;


}


var tab = newGlideAjax("Hr_case");


tab.addParam("sysparm_name", "getCaseetails");


tab.addParam("sysparm_number", g_form.getValue('name_of_case'));


tab.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


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


alert(answer);


if(answer == False )


alert('The case number doesnot exist');


}


}

======================

script include
var Hr_case = Class.create();


Hr_case.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCasedetails: function(){


var newtab = new GlideRecord('Hr_case');


newtab.addQuery('number', this.getParameter('sysparm_name_of_case'));


newtab.query();


if(newtab.next())


return true;


},


type: 'Hr_case'


});

 

I believe there are couple of mistakes here.. Not sure what.. 

 

variable field is: name_of_case

table: Hr_case.