how to create user name with first and last name and some random number after submit the form

Madala Chaitany
Giga Guru

Hi,

When a new user is created automatic user name should populate in the user name field using the first two letters of the first name and last name and some random number between 0 to 1.

 

Thanks in Advance.

Regards,

Chaitanya

1 ACCEPTED SOLUTION

Hemant Goldar
Mega Sage

Hello Chaitanya,

 

Write a Business rule on sys_user table with the condition below

find_real_file.png and here is the code 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var fname=current.first_name;
	var lname=current.last_name;
	
	var x = Math.floor((Math.random() * 2) + 1); //Generates random number from 1-2
	var username=fname+'.'+lname+x;
	current.user_name=username;
	current.update();
	gs.log(username);
})(current, previous);

 

I hope this helps!

 

Mark answer Correct and helpful if this solves your issue.

 

Thanks,

Hemant

View solution in original post

10 REPLIES 10

It will work only when a new user record is created. Can you tell me what issue you are facing??

I had checked with the new record only.

 

I had tried script like that

var fname=current.first_name;
var lname=current.last_name;

var x = Math.floor((Math.random() * 10)); //Generates random number from 1-9
var username=fname+'.'+lname+x;
current.user_name=username;
current.insert();

for this i getting dispaly with value and getting an error msg like java.sql.BatchUpdateException: (conn=22840) Duplicate entry 'aeab95c8dbb0f01086231d63059619fe' for key 'PRIMARY' and invalid insert 

 

And not saving the record

Try this

var x=Math.floor((Math.random()*10)+1)

 

And as it's a Math random number it can generate any thing.

 

Mark the answer correct and helpful.

 

Thanks

Hemant

Same issue I am getting check-in the screenshot below

Thanks, Hemant

It working