How to check if user id is available in sys_user table via script

SNOW39
Tera Expert

We have three fields in my catalog item . List of user(multiline text), user available, user not available.

When end user enter bulk number of user_ids with coma separated   (Eg: userid01,userid02,userid03,userid04,userid05,userid06,...) i should separate user ids that are available and not available in sys_user table and those available should be shown in user available and not availble users should be shown in user not available.

 

How to achieve this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script on multi-line text variable and you can filter out in respective fields

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkUsers: function(){

		var arr = this.getParameter('sysparm_userID').toString().split(',');            
		var foundArr = [];
		var notFoundArr = [];
		for(var i in arr){
			var gr = new GlideRecord('sys_user');
			gr.addQuery('user_name', arr[i]);
			gr.query();
			if(gr.hasNext()){
				foundArr.push(arr[i].toString());
			}
			else{
				notFoundArr.push(arr[i].toString());
			}
		}

		var obj = {};
		obj["found"] = foundArr.toString();
		obj["not_found"] = notFoundArr.toString();

		return JSON.stringify(obj);

	},

	type: 'checkRecords'
});

onChange client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkUsers");
	ga.addParam('sysparm_userID', g_form.getValue('list_of_user')); // give here correct multi line variable name
	ga.getXMLAnswer(function(answer){
		var parser = JSON.parse(answer);
		g_form.setValue('user_available', parser.found); // give correct variable name here
		g_form.setValue('user_not_available', parser.not_found); // give correct variable name here

	});
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Markus Kraus
Kilo Sage

Hi, given u have all the userIDs properly comma seperated, the following script should work (note: even though the column label is 'User ID', the column name is actually 'user_name'):

var available = [];
var unavailable = [];
var userIDs = 'userid01,userid02,userid03,userid04,userid05,userid06';

var userGr = new GlideRecord('sys_user');
userGr.addQuery('user_name', 'IN', userIDs);
userGr.query();
while (userGr.next()) {
  available.push(userGr.getValue('user_name'));
}

userIDs.split(',').forEach(function (userId) {
  if (available.indexOf(userId) == -1) {
    unavailable.push(userId);
  }
});

gs.info('available: ' + JSON.stringify(available));
gs.info('unavailable: ' + JSON.stringify(unavailable));

Hi Markus,

This userid are coming form service portal and the number of userid entered will be bulk.

Also, once checked the those available should be shown in user available field and not availble users should be shown in user not available field.

In this case use the following Client Script (onChange / Variable = List of user)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('UserIDCheckHelper');
	ga.addParam('sysparm_name', 'checkUserIDs');
	ga.addParam('user_ids', newValue.replace(/\s/g, '')); // remove whitespaces
	ga.getXMLAnswer(function (result) {
		result = JSON.parse(result);
		g_form.setValue('user_available', result.exist);
		g_form.setValue('user_not_available', result.nexist);
	});
}

And this Script Include (Client callable = true):

var UserIDCheckHelper = Class.create();
UserIDCheckHelper.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	checkUserIDs: function () {
		var result = {
			exist: [],
			nexist: []
		};

		var userIDs = this.getParameter('user_ids');
		var userGr = new GlideRecord('sys_user');
		userGr.addQuery('user_name', 'IN', userIDs);
		userGr.query();
		while (userGr.next()) {
			result.exist.push(userGr.getValue('user_name'));
		}

		userIDs.split(',').forEach(function (userId) {
			if (result.exist.indexOf(userId) == -1) {
				result.nexist.push(userId);
			}
		});
		
		return JSON.stringify(result);
	},

    type: 'UserIDCheckHelper'
});

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onChange client script on multi-line text variable and you can filter out in respective fields

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkUsers: function(){

		var arr = this.getParameter('sysparm_userID').toString().split(',');            
		var foundArr = [];
		var notFoundArr = [];
		for(var i in arr){
			var gr = new GlideRecord('sys_user');
			gr.addQuery('user_name', arr[i]);
			gr.query();
			if(gr.hasNext()){
				foundArr.push(arr[i].toString());
			}
			else{
				notFoundArr.push(arr[i].toString());
			}
		}

		var obj = {};
		obj["found"] = foundArr.toString();
		obj["not_found"] = notFoundArr.toString();

		return JSON.stringify(obj);

	},

	type: 'checkRecords'
});

onChange client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkUsers");
	ga.addParam('sysparm_userID', g_form.getValue('list_of_user')); // give here correct multi line variable name
	ga.getXMLAnswer(function(answer){
		var parser = JSON.parse(answer);
		g_form.setValue('user_available', parser.found); // give correct variable name here
		g_form.setValue('user_not_available', parser.not_found); // give correct variable name here

	});
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader