Check if username exists, and if so then increment by one number at the user name end

vikas50
Tera Contributor

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"

1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

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

View solution in original post

14 REPLIES 14

Rajesh Chopade1
Mega Sage

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.

In order to achieve this I have used Client script and Script Include

vikas50_0-1711012047533.png

vikas50_2-1711012252390.png

 

Help me out where I need to make changes

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.

Hi @vikas50 

Have you tried above solution, I hope this will helps you to resolve your issue.

 

Thank you.