Display error if no match found

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2022 09:18 AM
Hello,
I am trying to add field validation to one of our forms. Currently the field shows only client codes that have been open/active in the past 18 months. So if there are none that meet this criteria, then the error message should appear under the field.
It is a refrence filed type and pulling information from custom table.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2022 09:25 AM
If you want to display an error, you will need to run the same query though GlideAjax, reference qual won't be able to throw a field message if it doesn't return any value
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2022 05:49 PM
Hi Sparkles,
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var ajax = new GlideAjax('CustomTable');
ajax.addParam('sysparm_name', 'hasRecord');
ajax.addParam('sysparm_key', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0 && answer !='true') {
g_form.showFieldMsg('reference_field', 'There is no records.', 'info');
} else {
g_form.hideFieldMsg('reference_field');
}
});
}
var CustomTable = Class.create();
CustomTable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRecord: function() {
var key = this.getParameter('sysparm_key');
var grCustomTable = new GlideRecord('x_custom_table');
grCustomTable.addEncodedQuery('engagement_close_dateRELATIVEGT@month@aga@18^ORengagement_close}_dateISEMPTY^current_engagement_status_desc!=^opportunity="+key'); // use reference qualitifer condition
grCustomTable.query();
return grCustomTable.hasNext();
},
type: 'CustomTable'
});