How to generate random string and integer in server action.

How to generate a random string and integer in server action. To add excel file name unique value while extracting the file.

Hi Sachin,

You can check the built-in Guid function.
It generates a random Guid string. It's a long text however you can use substring to minimize it.

https://success.outsystems.com/Documentation/10/Reference/OutSystems_APIs/System_Actions#GenerateGuid

Hope it helps.

With best regards,
F.Karatay

@Ferhat: That's bad advice! GUIDs are unique, but they are definitely not random! And taking a substring of a GUID is even worse, as parts of it are explicitly not unique!

@Sachin: There are several Forge components that allow you to generate random integers and/or strings.

Kilian Hekhuis wrote:

@Ferhat: That's bad advice! GUIDs are unique, but they are definitely not random! And taking a substring of a GUID is even worse, as parts of it are explicitly not unique!

@Sachin: There are several Forge components that allow you to generate random integers and/or strings.

Hello Killian,

  Instead of forge component can we run javascript and get the random value? In server action.

Regards,

Sachin


Hi Sachin,

JavaScript runs client side. It doesn't run server side, there's no server side JavaScript with the OutSystems Platform. You cannot run JavaScript in a Server Action*. Why would you want to use JavaScript?

*Yes, there's RunJavaScript from the HTTPRequestHandler, but it doesn't do what you'd probably expect.

Kilian Hekhuis wrote:

Hi Sachin,

JavaScript runs client side. It doesn't run server side, there's no server side JavaScript with the OutSystems Platform. You cannot run JavaScript in a Server Action*. Why would you want to use JavaScript?

*Yes, there's RunJavaScript from the HTTPRequestHandler, but it doesn't do what you'd probably expect.


Hi Killian,


Without Forge components, Not there any builtin function? Like as suggested Ferhat


Regards,

Sachin

No, there's nothing built-in to use. But there's nothing wrong with using Forge Compents!

Ferhat Karatay wrote:

Hi Sachin,

You can check the built-in Guid function.
It generates a random Guid string. It's a long text however you can use substring to minimize it.

https://success.outsystems.com/Documentation/10/Reference/OutSystems_APIs/System_Actions#GenerateGuid

Hope it helps.

With best regards,
F.Karatay


Hello Ferhat,

Even unique id also fine, Working as excepted Thank you.

Yes, if you need just a unique, not a random, sequence of characters, using the built-in GenerateGuid is fine. But like I said, it is not random, and you should not expect parts of it to be random.

Kilian Hekhuis wrote:

No, there's nothing built-in to use. But there's nothing wrong with using Forge Compents!


Hi Kilian,

 

Thank's for the information.


Outsystems already has built in functionality to generate random passwords.

It takes 2 parameters... the number of character you want and if it should include letters as well as numbers:


https://success.outsystems.com/Documentation/11/Reference/OutSystems_Language/Logic/Built-in_Functions/Miscellaneous

Hi, I am building a mobile screen to:

1. Generate a random number 

2. Add a record to a table with that random number

3. Display that random number on screen

I read the doc link above but I am not able to find the GeneratePassword function. Could you please give me some pointers how/where to insert this function call in the 1st step?

Thanks!

Herman

Hi,

You do not see the function as it is a build in function only available in server actions.

If you want to generate a random string of length of your choice in JavaScript, you can add a JavaScript node with the following code and and an input parameter TextLengthnof type integer and an output parameter of type text called Uid to it.

Regards,

Daniel

function getRandomString(length) {
  var s = '';
  do { s += Math.random().toString(36).substr(2); } while (s.length < length);
  s = s.substr(0, length);
  
  return s;
}

$parameters.Uid = getRandomString($parameters.TextLength);

Hi Daniel,

Thanks for your answer. I am quite new to OutSystems and am still feeling things around. 

On that screen, I should add Input, Output Param, a ClientAction on Initialize. Inside the ClientAction, I should add the Javascript node, and then the CreateOrUpdate Server Action to insert the record?

Herman

Herman Z wrote:

Hi Daniel,

Thanks for your answer. I am quite new to OutSystems and am still feeling things around. 

On that screen, I should add Input, Output Param, a ClientAction on Initialize. Inside the ClientAction, I should add the Javascript node, and then the CreateOrUpdate Server Action to insert the record?

Herman


Hi Herman,


You need to add the input and output parameter for the javascript node that you use in your client action.


Regards,

Clement

Hi Clement,

Here is a demo mobile app in which it is implemented.

I made it a reusable client action that is used in the screen.

Regards,

Daniel

Thank you Clement and Daniel for your answers and Daniel, you even posted a .oap, thanks a lot!!

I am able to solve the problem I have.

Off topic, if I have to insert a record into a table on mobile screen load, what's a good practice? Run a SQL stored procedure or use the CreateXXX function?

Herman

Hi,

Glad I could help.

Best practice is to use the entity actions that OutSystems provide. 

Regards,

Daniel