GlideAjax question

tanz
Tera Expert

I want to write a client script which will perform some actions (display alert that number or date is empty ) in catalog item when the number and date fields from table xyz .

 

For this GlideAjax script should be written which should check on the records and return some flag if one of fields in empty.

 

i want to know how to achieve this.

var gr = new GlideRecord('xyz');
gr.addQuery('sys_id','IN' ,sysIds); // pass the sysids in the glideajax query param
gr.query();
{ if any one of the number or date is empty 
return which field is empty against which number
}
In client script 
alert those and do some action
The sys ids will be multiple like sys id in sysid1,sysid2,sysid3
as the field in client script is list collector in which user can input multiple records from xyz table
8 REPLIES 8

My client side has list collector field from where i am fetching multiple sys ids of records. I want to pass these sysids to server side glideajax and display message on which records the number and date is empty return those as alert msgs

var sysIds = this.getParameter('sysparm_query');
        var grrec = new GlideRecord('xyz');
        grrec.addQuery('sys_id', 'IN', sysIds); // multiple sys ids
        grrec.query();

        while (grrec.next()) {
            if (!grrec.number|| !grrec.xdate) {
                return 'false'+ grrec.number;
            }
        }
        return 'true';
    },
But this is always returning true while trying to alert the answer for the records which has empty date and number also

Can you try this below code,

 

var sysIds = this.getParameter('sysparm_query').split(','); // Split the sys_ids into an array
var grrec = new GlideRecord('xyz');
grrec.addQuery('sys_id', 'IN', sysIds); // multiple sys ids
grrec.query();

while (grrec.next()) {
    if (grrec.number.nil() || grrec.xdate.nil()) {
        return 'false: ' + grrec.number;
    }
}
return 'true';

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla

 

Ambuj Tripathi
ServiceNow Employee
ServiceNow Employee

Yes, checking for negation could be the reason. We can either check for nil or add additional query to avoid fetching the unmatched records as well.
Other issue I am seeing with this is it will not return all the sys_ids. Instead, can store all the required sys_ids at once and return it after the loop processing is complete like this-

function test(){
	var sysIds = 'c0cf7954874023003c1c8467a7cb0b54, 1c832706732023002728660c4cf6a7b9';
	var grrec = new GlideRecord('incident');
	grrec.addQuery('sys_id', 'IN', sysIds); // multiple sys ids
	grrec.addNullQuery('action_status');
	grrec.addNullQuery('activity_due');
	grrec.query();
	if(!grrec.hasNext())
		return true;

	var abc = [];
	while (grrec.next()) {
		if (!grrec.action_status|| !grrec.activity_due) {
			abc.push('false'+ grrec.number);
		}
	}
	return abc;
}

gs.log(test());