Issue passing text split into script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
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(', ');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
53m ago
try this
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
48m ago
Hello @ChinchillaMoMo , Good Day!!!
Please try below script include. It is Client callable script include.
Thanks & Regards,
Sourabh A.
