- 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-19-2024 10:31 AM
Hi @vikas50
I have created and checked following code in background script, do needful modification and use same to solve your issue:
var firstName = 'Rajesh ';
var lastName = 'Aluguri1';
var vr = new GlideRecord('sys_user');
vr.addQuery('name',firstName+lastName );
vr.query();
while(vr.next()){
var userName = vr.name;
gs.print('Name = '+userName );
var lastChar = userName.slice(-1);
// check last character is integer or not
var pattern = /^[0-9]*$/;
if(pattern.test(lastChar)){
// increase integer by 1 and add at the end of name
lastChar++;
gs.print('USER NAME if '+userName.replace(/.$/, '')+lastChar);
}else{
// Add integer 1 at the end of name if no number found in name
gs.print('USER NAME '+userName +'1');
}
}
I hope my answer helps you to resolve your issue, please mark this correct and helpful accordingly.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 02:09 AM - edited ‎03-21-2024 02:12 AM
In order to achieve this I have used Client script and Script Include
Help me out where I need to make changes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 03:50 AM
Hi @vikas50
You just need to add following code in your client script in IF block:
var lastChar = Lname.slice(-1);
// check last character is integer or not
var pattern = /^[0-9]*$/;
var finalName ='';
if(pattern.test(lastChar)){
// increase integer by 1 and add at the end of name
lastChar++;
finalName = Fname+' '+Lname.replace(/.$/, '')+lastChar);
}else{
// Add integer 1 at the end of name if no number found in name
finalName = Fname+' '+Lname +'1');
}
// You can utilize 'finalName' as per your requirement.
I hope my code resolve your issue, please mark my answer correct and helpful accordingly.
thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2024 05:33 AM
Hi @vikas50
Have you tried above solution, I hope this will helps you to resolve your issue.
Thank you.