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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Tejas Adhalrao
Tera Guru

Hi @ChinchillaMoMo  ,

You are entering multiple employee IDs in a multi-line text field in Service Portal.

ex:A00001
B00001

But when the value reaches the Script Include, it looks like one single string, not separate lines.

 

you can try with this code 

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

if (!inputString)
    return '';

// Split input by new line (Enter key)
var idArray = inputString.split(/\r?\n/);

var invalidIds = [];

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

    var currentId = idArray[i].trim();
    if (!currentId)
        continue;

    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('employee_number', currentId);
    userGr.setLimit(1);
    userGr.query();

    if (userGr.next()) {
        gs.info('[UserValidation] Employee found: ' + currentId);
    } else {
        invalidIds.push(currentId);
        gs.info('[UserValidation] Employee NOT found: ' + currentId);
    }
}

return invalidIds.join(', ');

 

 

 

 

 

vaishali231
Tera Guru

hey @ChinchillaMoMo 

try this :

validateUsers: function () {

    var inputString = this.getParameter('sysparm_user_ids');
    if (gs.nil(inputString)) {
        return '';
    }

    // Normalize line breaks
    inputString = inputString.replace(/\r\n/g, '\n');

    // Split by new line, comma, or space
    var idArray = inputString.split(/[\n,\s]+/)
        .map(function (id) {
            return id.trim();
        })
        .filter(Boolean);

    gs.info('[UserValidation] Parsed IDs: ' + idArray.join(', '));

    var invalidIds = [];

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

        var currentId = idArray[i];

        var userGr = new GlideRecord('sys_user');
        userGr.addQuery('employee_number', currentId);
        userGr.setLimit(1);
        userGr.query();

        if (userGr.next()) {
            gs.info('[UserValidation] Employee ID FOUND: ' + currentId);
        } else {
            invalidIds.push(currentId);
            gs.info('[UserValidation] Employee ID NOT FOUND: ' + currentId);
        }
    }

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

*************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

Hey @ChinchillaMoMo 

 

Hope you are doing well.

Did my previous reply answer your question?

If it was helpful, please mark it as correct ✓ and close the thread 🔒. This will help other readers find the solution more easily.

Regards,
Vaishali Singh

Tried with your scripts but unfortunately it doesn't work