- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 02:33 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 02:43 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 02:38 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 02:43 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 04:30 AM
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 04:39 AM
Glad to help.
Please remember to mark response helpful as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader