The CreatorCon Call for Content is officially open! Get started here.

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

Hi @vikas50 

 

I tried to solve your ask or question via giving 2 solutions: -

  1. Using Client Script and Script Include.
  2. 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

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

I was checking the user in catalog form based on the first name and last name by using script include and client script

vikas50_0-1711014544503.pngvikas50_1-1711014566829.png

 

Hi @vikas50 

 

Can you try my approach? I am not sure if client scripts can do.

 

Regards,

Amit

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.

vikas50_0-1711105120429.pngvikas50_1-1711105137267.png