The Zurich release has arrived! Interested in new features and functionalities? Click here for more

create userid on the custom table

huyjor
Tera Contributor

Hello

I have a custom table with the fields for: userid, firstname and last name. If the user fills out the form for first name and last name then hit submit. I want automatically generate  the userid on the field in the table. I used business rule for this. I'm not sure why it's not generating the userid. Please help ! 

Thank You

 

When to run: After. Method: Insert

 

code below:

(function executeRule(current, previous /*null when async*/) {
 
var fname = current.first_name;
var lname = current.last_name;
 
var cal = Math.floor((Math.ramdom() * 2) +1); //Generate random number from 1-2
var username = fname+ '.'+lname +cal;
current.userid=username;
current.update();
gs.log(userid);
 
})(current, previous);
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Looks like there is a spelling issue

var cal = Math.floor((Math.ramdom() * 2) +1); 

instead of ramdom, use random.

You may also want run it before insert, so that you dont need a current.update().

Also may need to check, there are no existing users with the same username.


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Looks like there is a spelling issue

var cal = Math.floor((Math.ramdom() * 2) +1); 

instead of ramdom, use random.

You may also want run it before insert, so that you dont need a current.update().

Also may need to check, there are no existing users with the same username.


Please mark this response as correct or helpful if it assisted you with your question.

It works. It generated the userid in the field. But i'm running into an issue that it's generating the same id for the person who has the same first name and last name. I tried to modify the code: var cal = Math.floor((Math.ramdom() * 100) +1); How do you make it unique so it wouldn't have duplicate userid later. 

 

Thanks 

I would multiply by 1000, for ex i get more unique numbers

var cal = Math.floor(Math.random() * 1000);
Or do alphanumeric
Math.random().toString(16).slice(2)
 

Please mark this response as correct or helpful if it assisted you with your question.

Thanks so much !