- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2024 09:23 AM
I want to check if a user name exists in the user table and the user name should be a combination of first and last names. If it exists then add a number to the end of the last name starting from +1
Example: "Mark Henry" already exists try ''Mark Henry1'' if ''Mark Henry1'' exists then create "Mark Henry 2"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 02:13 AM
Hi @vikas50
Would encourage you to create before/insert business rule on sys_user table with following script-
(function executeRule(current, previous /*null when async*/ ) {
if (current.isNewRecord()) {
var firstName = current.first_name;
var lastName = current.last_name;
var userName = firstName + ' ' + lastName;
var userGr = new GlideRecord('sys_user');
userGr.addQuery('user_name', userName);
userGr.query();
if (userGr.next()) {
var newName = userName;
var count = 1;
do {
newName = userName + count;
userGr.initialize();
userGr.addQuery('user_name', newName);
userGr.query();
count++;
} while (userGr.next());
current.user_name = newName;
} else {
current.user_name = userName;
}
}
})(current, previous);
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 04:07 AM
Hi @vikas50
I tried to solve your ask or question via giving 2 solutions: -
- Using Client Script and Script Include.
- Using Business Rule
You can have a look at once and might be useful for you to get the solution in one go.
If you find any of the answer/solution/suggestion as helpful to your question asked or meet with your requirement, you can mark the solution/answer/suggestion as helpful, and correct.
Thanks
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 02:13 AM
Hi @vikas50
Would encourage you to create before/insert business rule on sys_user table with following script-
(function executeRule(current, previous /*null when async*/ ) {
if (current.isNewRecord()) {
var firstName = current.first_name;
var lastName = current.last_name;
var userName = firstName + ' ' + lastName;
var userGr = new GlideRecord('sys_user');
userGr.addQuery('user_name', userName);
userGr.query();
if (userGr.next()) {
var newName = userName;
var count = 1;
do {
newName = userName + count;
userGr.initialize();
userGr.addQuery('user_name', newName);
userGr.query();
count++;
} while (userGr.next());
current.user_name = newName;
} else {
current.user_name = userName;
}
}
})(current, previous);
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 02:51 AM
I was checking the user in catalog form based on the first name and last name by using script include and client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 03:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2024 04:13 AM - edited ‎03-22-2024 04:21 AM
Hi @Amit Pandey ,
I have configured a catalog form to perform validation using client script and script include. currently it is checking the user exists or not , if doesn't exist, it populates the user name combination of first name and last name but in my case I need the script should also check if a user with the same first name and last name combination already exists. If it does, the script should append a number to the end of the last name.