Create unique username

kennethandersen
Kilo Contributor

I will like to use the transform map to create a unique username from firstname an lastname plus a number 3+3+4

So a name like this

Ex. 1 # Lars Larsen will be LAR (3 from firstname) LAR (3 from lastname) 1985 (a random number)

So the username is LARLAR1985

Ex. 2 # Lars W. Larsen will be LAR (3 from firstname) LAR (3 from lastname) 1985 (a random number)

So the username is LARLAR1856

But before the creating of the username, it have to check the sys_user table if the username is already where.

Hope you got the idea

1 ACCEPTED SOLUTION

With the assumptions that the import only creates new user records and the first and last field names try this in a field map script.



var first = source.u_firstname.slice(0,3);


var last = source.u_lastname.slice(0,3);


var uid = first + last + Math.floor((Math.random() * 9999) + 1000);




var i = 0;


var gr = new GlideRecord('sys_user');


while (i == 0){


  if (gr.get('user_name', uid)){


        uid = first + last + Math.floor((Math.random() * 9999) + 1000);


  } else {


        i = 1;


  }


}



answer = uid;


View solution in original post

8 REPLIES 8

rlatorre
Kilo Sage

Is this import only to create new users?


With the assumptions that the import only creates new user records and the first and last field names try this in a field map script.



var first = source.u_firstname.slice(0,3);


var last = source.u_lastname.slice(0,3);


var uid = first + last + Math.floor((Math.random() * 9999) + 1000);




var i = 0;


var gr = new GlideRecord('sys_user');


while (i == 0){


  if (gr.get('user_name', uid)){


        uid = first + last + Math.floor((Math.random() * 9999) + 1000);


  } else {


        i = 1;


  }


}



answer = uid;


Thanks. Its looking nice



I did forgot to write that we have special danish character, so I have í…í˜í† and åøæ in the username, is there a way to change the character to this:



í… = A


í˜ = O


í† = A


Ã¥ = a


ø = o


æ = a


Or better have a list over accepted character a-z.




Can I use this one?



I find this on google.


By adding our .cleanup() method to the String object itself, you can then cleanup any string in Javascript simply by calling a local method, like this:


# Attaching our method to the String Object String.prototype.cleanup = function() { return this.toLowerCase().replace(/[^a-zA-Z0-9]+/g, "-"); }   
# Using our new .cleanup() method var clean = "Hello World".cleanup(); // "hello-world"