- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 02:39 PM
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
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 03:13 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2016 07:32 AM
Give it a shot when defining first and last.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2016 02:56 PM
U can achieve this is a transform script. Make a onBefore and do the check, and before you decide on the name, you do a query to the usertable to see if it already exist. If not, use it, otherwise just add "reroll" the random number and check again.
//Göran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2016 01:32 PM
Maybe like this ?
String.prototype.cleanup = function() {
return this.toLowerCase().replace(/[^a-z']/g, '');
};
var first = source.u_firstname.slice(0,3).cleanup();
var last = source.u_lastname.slice(0,3).cleanup();
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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2022 06:27 AM
If you also want to limit to total length of the user name, this function may be helpful:
The DevTools app contains a truckload of reusable scripts and
features to support your app development.
Fork at will!