How to get different userID and email for the same fname, lname wen submitted the record producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2023 02:36 AM
Need script to update userid as userid, userid0 , userid1 and email as test.abc@gmail.com , test.abc0@gmail.com etc.
each time when we submit the form using same first name and last name, as organisation may have multiple users having same name. So that there wont be any duplicates.
When im trying to code, values are getting updated in the same profile, but not generating new profiles and new userids and email like userid0,userid1 , test.abc0@gmail.com etc
Attached OOTB script include(hr_sysUser), where the logic of userid from firstname and lastname mentioned.
Userid code :
(function executeRule(current, previous /*null when async*/) {
var fname = current.first_name;
var lname = current.last_name;
var firstLetter = fname.slice(0,1);
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
var userName = firstLetter + lname +suffix;
current.user_name=userName;
current.update();
}
}
Email Field code:
(function executeRule(current, previous /*null when async*/) {
var first = current.first_name;
var last = current.last_name;
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
var userName = first + "." + last + suffix + '@gmail.com';
current.email=userName;
current.update();
}
})(current, previous);
- Labels:
-
HR Service Delivery

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 12:58 AM
Remove the excess clutter from your code and use the following script inside an On Insert Before business rule on sys_user table.
(function executeRule(current, previous /*null when async*/ ) {
//Generate email for the user
// Add your code here
var email = generateEmail(current.first_name + '', current.last_name + '', '@example.com');
var userName = generateUserName(current.first_name + '', current.last_name + '');
if (email && userName) {
current.email = email + '';
current.user_name = userName + '';
}
function generateUserName(first_name, last_name) {
var userName = first_name.slice(0, 1) + "." + last_name;
var record = new GlideRecord('sys_user');
if (!record.get("user_name", userName))
return userName.toLowerCase();
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
record = new GlideRecord("sys_user");
userName = first_name.slice(0, 1) + "." + last_name + suffix;
if (!record.get("user_name", userName))
return userName.toLowerCase();
}
gs.debug("Cannot generate user name for " + first_name + ", " + last_name);
return null;
}
// Add your code here
function generateEmail(first_name, last_name, domain) {
var email = first_name + "." + last_name + domain;
email = email.toLowerCase();
var record = new GlideRecord('sys_user');
if (!record.get("email", email))
return email;
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
record = new GlideRecord("sys_user");
email = first_name + "." + last_name + suffix + domain;
email = email.toLowerCase();
if (!record.get("email", email))
return email;
}
gs.debug("Cannot generate user email for " + first_name + ", " + last_name);
return null;
}
})(current, previous);
What this BR does is, it updates the value of user name and email fields based on your criteria and puts the updated value on the current sys_user object which is going to be inserted in the DB.
I am also attaching the XML of this record for your reference.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 01:26 AM
Hi Sandeep,
Tried the above one, email id and user id are getting created. First part is fine.
But its not entering the suffix loop, if i submit with any existing first name and last name, new user profile with userid/email along with suffix are not getting created. Its showing the same profile again without any suffix.
I think its not checking the existing user_name/email to enter the suffix loop properly. Are we missing any condition before it enters the for suffix loop.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 02:33 AM
For me the code is running just fine. Tested it with background script and it suggested correct username and email combinations.
I recommend you to run the following script inside your instance and see if it produces desired results.
//Generate email for the user
// Add your code here
var email = generateEmail('abel' + '', 'tuter' + '', '@example.com');
var userName = generateUserName('abel' + '', 'tuter' + '');
if (email && userName) {
gs.print('suggested email is '+ email);
gs.print('suggested user name is '+ userName);
}
function generateUserName(first_name, last_name) {
var userName = first_name.slice(0, 1) + "." + last_name;
var record = new GlideRecord('sys_user');
if (!record.get("user_name", userName))
return userName.toLowerCase();
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
record = new GlideRecord("sys_user");
userName = first_name.slice(0, 1) + "." + last_name + suffix;
if (!record.get("user_name", userName))
return userName.toLowerCase();
}
gs.debug("Cannot generate user name for " + first_name + ", " + last_name);
return null;
}
// Add your code here
function generateEmail(first_name, last_name, domain) {
var email = first_name + "." + last_name + domain;
email = email.toLowerCase();
var record = new GlideRecord('sys_user');
if (!record.get("email", email))
return email;
var count = (gs.getProperty("sn_hr_core.profile.max.tries", 50));
for (var suffix = 0; suffix < count; ++suffix) {
record = new GlideRecord("sys_user");
email = first_name + "." + last_name + suffix + domain;
email = email.toLowerCase();
if (!record.get("email", email))
return email;
}
gs.debug("Cannot generate user email for " + first_name + ", " + last_name);
return null;
}
In this script replace 'abel' 'tuter' and '@example.com' with your user's first name, last name and email domain. It will work just fine if you run it on your personal development instance with demo data without making any changes.
Also, check your business rule when to run conditions, the script mentioned here should only run on insert and do not use current.update() in it. Since this is an onBefore insert script it will update the values for the user name and email field without needing for a current.update() call.
Also share the snap-shot of your BR if possible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 04:06 AM
Hey Sandeep,
Background script is running fine when i gave existing first name and last name from the system. It suggested the userid and email as needed.
But when i submit the RP form with existing first name and last name and when HR case gets generated, it just updating the existing user profile details, but not creating a new user profile with new userid and email
Attached my BR snapshots.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2023 05:09 AM
Hi Sandeep,
Script is running fine but what i noticed is, user profile with new userid and email is not getting created when i submit the record producer form.
I tried to directly create new user with existing first name and last name, new userprofile with userid and email created ..its working fine
Just need to understand the linkage between record producer and user profile creation, and how can we generate a new user profile as soon as we submit the form on esc.