- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 01:44 AM
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 🙂
Solved! Go to Solution.
- Labels:
-
Service Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2022 02:08 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 02:44 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2022 02:08 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2022 02:51 AM
Did you mistakenly marked your own response as correct?
Would you mind marking my response as correct as my code helped you.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader