Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Issue passing text split into script include

ChinchillaMoMo
Mega Guru

Hi community,

 

I have a service portal form the will capture a list of User employee Id input by user in a multi-line field. Here is a sample of the input data:

A00001

B00001

 

Then I'm trying to use a script include to check whether the employee id exists in the user table. I have some sample. But it seems the input data won't be separated into different lines. Any one can help?

 

var inputString = this.getParameter('sysparm_user_ids');

        if (!inputString) return '';

        // Clean the input: Split by new line, comma, or spaces
		var idArray = inputString.split(/[\n,\s]+/).map(function(item) {
            return item.trim();
        }).filter(Boolean);
		gs.info("[User Validation] input array: "+ idArray);

        var invalidIds = [];

        // Check database
        for (var i = 0; i < idArray.length; i++) {

			var currentId = idArray[i];
			gs.info("[UserValidation] unique id in loop: " + currentId);
			var userGr = new GlideRecord('sys_user');
            userGr.addQuery('employee_number', currentId); 
            userGr.setLimit(1);
            userGr.query();
        
			gs.info("[UserValidation] get user Id: " + userGr.getValue('name'));

           if (userGr.next()) {
            	gs.info("[UserValidation] User Employee Number Found: "+ currendId);
           }else{
				invalidIds.push(currentId);
				gs.info("[UserValidation] User Employee Number NOT Found: "+ currentId);
		   }
        }
		gs.info("[UserValidation] Invalid Ids: " + invalidIds);

        return invalidIds.join(', '); 
    },
    type: 'UserValidationUtils'

 Many thanks! 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@ChinchillaMoMo 

try this

var inputString = this.getParameter('sysparm_user_ids');

if (!inputString)
    return '';

// Clean the input: Split by new line, comma, or spaces
var idArray = inputString.toString().split('\n'); // try to split using \r\n if \n does not work
gs.info("[User Validation] input array: " + idArray);

var invalidIds = [];

// Check database
for (var i = 0; i < idArray.length; i++) {

    var currentId = idArray[i];
    gs.info("[UserValidation] unique id in loop: " + currentId);
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('employee_number', currentId);
    userGr.setLimit(1);
    userGr.query();

    gs.info("[UserValidation] get user Id: " + userGr.getValue('name'));

    if (userGr.next()) {
        gs.info("[UserValidation] User Employee Number Found: " + currendId);
    } else {
        invalidIds.push(currentId);
        gs.info("[UserValidation] User Employee Number NOT Found: " + currentId);
    }
}
gs.info("[UserValidation] Invalid Ids: " + invalidIds);

return invalidIds.join(', ');
},
type: 'UserValidationUtils'

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

 

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

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron

@ChinchillaMoMo 

try this

var inputString = this.getParameter('sysparm_user_ids');

if (!inputString)
    return '';

// Clean the input: Split by new line, comma, or spaces
var idArray = inputString.toString().split('\n'); // try to split using \r\n if \n does not work
gs.info("[User Validation] input array: " + idArray);

var invalidIds = [];

// Check database
for (var i = 0; i < idArray.length; i++) {

    var currentId = idArray[i];
    gs.info("[UserValidation] unique id in loop: " + currentId);
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('employee_number', currentId);
    userGr.setLimit(1);
    userGr.query();

    gs.info("[UserValidation] get user Id: " + userGr.getValue('name'));

    if (userGr.next()) {
        gs.info("[UserValidation] User Employee Number Found: " + currendId);
    } else {
        invalidIds.push(currentId);
        gs.info("[UserValidation] User Employee Number NOT Found: " + currentId);
    }
}
gs.info("[UserValidation] Invalid Ids: " + invalidIds);

return invalidIds.join(', ');
},
type: 'UserValidationUtils'

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

 

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

works with \r\n, thanks!

SourabhAkiwate
Tera Expert

Hello @ChinchillaMoMo , Good Day!!!

Please try below script include. It is Client callable script include.

 

checkUsers: function () {
        var users = this.getParameter('sysparm_users');
        if (!users)
        {
            return 'true';
        }
        var userArray = users.split(',');
        var missingUsers = [];
        for (var i = 0; i < userArray.length; i++)
        {
            var userId = userArray[i].trim();
            if (!userId) continue;
            var grUser = new GlideRecord('sys_user');
            grUser.addQuery('user_name', userId); // or employee_number
            grUser.query();
            if (!grUser.hasNext())
            {
                missingUsers.push(userId);
            }
        }
            if (missingUsers.length > 0)
            {
                return missingUsers.join(', ');
            }
            return 'true';
    },
 
 Output :
 SourabhAkiwate_0-1769771635267.png

 

Thanks & Regards,

Sourabh A.