339
Views
14
Comments
Solved
How to generate random alphanumeric numbers and store then in database
Application Type
Reactive
Service Studio Version
11.53.43 (Build 62091)

Hi folks

 I have a scenario wherein I need to generate random alphanumeric numbers and store them in database.

My scenario goes in like when I create a new student and add their details the seat number should be a random alphanumeric number around 8 digits and it should get stored directly in the database whenever I click on save button.

2023-03-16 04-58-56
Ashish Lonkar
Solution

Hi Athulya,

check out this demo, It will update new alphanumeric value everytime there is new entry and everytime there is update.

Thanks AWL!

Forum.oml
2022-08-23 08-18-03
Athulya Panicker

Thank you so much for the solution

2024-12-10 04-40-04
Gitansh Anand
Solution

Here are possible solutions to your requirement:

1. Generate only when create (Original answer)
If(Source.Id = NullIdentifier(), GeneratePassword(8, True), Source.Seat)

2. Generate new every time (create and update)
GeneratePassword(8, True)

3. Generate every time when update
If(Source.Id = NullIdentifier(), Source.Seat, GeneratePassword(8, True))

4. Generate only on first update
If(Source.Id <> NullIdentifier() and Source.Seat = NullTextIdentifier(), GeneratePassword(8, True), Source.Seat)

This assign is in the "StudentCreateOrUpdate" server action

2024-09-12 02-43-38
Deepsagar Dubey

Hi @Athulya Panicker 

You can use GeneratePassword(Integer, Boolean) action to generate alphanumeric numbers.


GeneratePassword(Integer, Boolean)Generates and returns a password string of length 'l'. If 'alpha' is True the string can contain both digits and letters, if False the string can only contain digits. The generated password string is a random combination of letters and digits, or just digits, depending on 'alpha'.


You can find complete guide here.

I hope it'll help you,

Thanks

Deep

2022-08-23 08-18-03
Athulya Panicker

Hi I was able to generate random numbers but I couldn't save it into the db by using server actions

2023-01-26 16-03-24
Ana Agostinho

Hello Athulya Panicker, 


So, the Generate password returns a password/integer string of length you have defined. 

After that you don't need something extraordinary and unusual like you already normally do to save data on the database. 

In resume, in order to save information/data on the database:

  • Create the entity you need to store the data (or in your case, add the attribute to store the alphanumeric numbers at the Student entity); 
  • Define the data type (maybe in your case it is better to store as integer and not text). You need to use some Data conversion function.
  • Use the entity action Create or CreateUpdate or Update (the one that fits your case better) 

These are some links that might help you:

I also recommend you review or do the guide paths: 

Including the exercises that include detailed instructions on how to do these things and much more, like bookings, employees or orders, for example. 

Best regards, 

Ana

2024-09-12 02-43-38
Deepsagar Dubey

Hi Athulya 

I don't think there will be any problem to save it, Just make sure your entity attribute type is test in which you want to save this random generated string.

Thanks

Deep

2024-12-10 04-40-04
Gitansh Anand

Hi @Athulya Panicker, I am adding an OML, please look to see if it helps.
I have used the GeneratePassword function, as @Deepsagar Dubey suggested, to save seat directly in the database.

Thanks
Gitansh Anand

Test.oml
2022-08-23 08-18-03
Athulya Panicker

Hi @Gitansh Anand 

The solution you've sent have worked perfectly but there is a slight change in my POC now, I want to generate alphanumeric numbers when I update table. What changes should I do in the condition to achieve it?

2024-12-10 04-40-04
Gitansh Anand

Do you need to generate new alphanumeric numbers every time the table is updated? Will you not generate alphanumeric numbers the first time you create in the table?

2024-12-10 04-40-04
Gitansh Anand
Solution

Here are possible solutions to your requirement:

1. Generate only when create (Original answer)
If(Source.Id = NullIdentifier(), GeneratePassword(8, True), Source.Seat)

2. Generate new every time (create and update)
GeneratePassword(8, True)

3. Generate every time when update
If(Source.Id = NullIdentifier(), Source.Seat, GeneratePassword(8, True))

4. Generate only on first update
If(Source.Id <> NullIdentifier() and Source.Seat = NullTextIdentifier(), GeneratePassword(8, True), Source.Seat)

This assign is in the "StudentCreateOrUpdate" server action

2022-08-23 08-18-03
Athulya Panicker

Actually in my POC there are 2 buttons 

1. button is to save details to the table

2. update the table while generating alphanumeric value.

Since I need to use the second button for updating the table and generating values I asked the doubt.

2024-09-12 02-43-38
Deepsagar Dubey

Hi @Athulya Panicker 

In your second button click action pass the actual record Id which you stored in database by clicking on first button,

Then use the GeneratePassword(Integer, Boolean)  action to generate alphanumeric value and use the updateEntity action to update record in your table.

Thanks

Deep  

2024-12-10 04-40-04
Gitansh Anand

In that case, create a new action, call this action to the 2nd button, add the GeneratePassword function to update the value of Seat in the source, and then call the original action. Also remove the GeneratePassword function used in the original function.
This way, the first button will work as it is (without the alphanumeric value), and the second button will first create a new alphanumeric value and then work as it is.

Thanks
Gitansh Anand

2022-02-25 19-42-59
Srinivas K Singadi
AI Generated

Hello Athulya Panicker

  • Create a server action by right-clicking on the Logic folder in the Solution Explorer and selecting "Add New Server Action".
  • In the new server action, add an "Assign" node and create a local variable of type "Text" to hold the generated random alphanumeric value.
  • In the "Assign" node, use the "GenerateRandomString" function to generate the alphanumeric value. The function takes two parameters: the length of the string you want to generate and a boolean value to indicate whether to include numbers in the generated string. For example, to generate a random string of length 10 that includes both letters and numbers, you can use the following expression: "GenerateRandomString(10, True)".
  • After generating the random string, you can store it in the database by using a database action to insert a new record into the relevant table. Add a "CreateOrUpdate" node to your server action and configure it to insert a new record into the relevant table. Map the generated random string to a field in the table using the appropriate input parameter.
  • Save your server action and call it from the appropriate screen or other server action to generate and store the random alphanumeric value in the database
This answer was AI-generated. Please read it carefully and use the forums for clarifications
2023-03-16 04-58-56
Ashish Lonkar
Solution

Hi Athulya,

check out this demo, It will update new alphanumeric value everytime there is new entry and everytime there is update.

Thanks AWL!

Forum.oml
2022-08-23 08-18-03
Athulya Panicker

Thank you so much for the solution

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.