Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Display error if no match found

sparkles
Tera Contributor

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. 

 

 

 

find_real_file.png

2 REPLIES 2

Aman Kumar S
Kilo Patron

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

Best Regards
Aman Kumar

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Sparkles,

 

 
Since the reference qualifier is based on another field "opportunity_id_add", it is necessary to create an onChange Client Script on that field.
 
Client Script
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');
		}
    });
 }
Script Include
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'
});
find_real_file.png
Execution results
case 1: There is no matching record
find_real_file.png
case 2: There is a matching record
find_real_file.png