597
Views
5
Comments
Solved
Random item from a list/aggregate

Afternoon all, i am attempting to pick a random record from an aggregate to then be put into a variable. however I am unsure how to do this. picking a random identifier would be suitable but just not sure the process. any help is welcome. TIA

2023-10-21 19-42-11
Tousif Khan
Champion
Solution

Hi Maxwell,

You can also achieve it through JavaScript as well you can generate a random number using random() function first you need to get the total count of the aggregate then pass it in your JavaScript through input parameter

it will return a  random number, you can use it to fetch the data from aggregate.

I have attached OML for the reference.

Sample- Link


I hope this will help

Thanks 

Tousif khan

Random_Record_Sample_Oml.oml
2023-03-16 16-29-51
Paulo Rosário

Hello Maxwell,

I would suggest not picking a random identifier but maybe a random index of the list.

An easy way to do this is  : 

  1. Check how many items you have on the list using the Length attribute of your aggregate.
  2. Generate a random number between 0 and your list length -1( this is to ensure that the index exists ), you can use this component from the forge to do it.
  3. Then you the following notation on your assign to put it into your variable 
    1. Yourlist.List[randomIndexNumber].YourEntity

Hope it helps!

Paulo Rosário 

UserImage.jpg
Anshal Anwesh

Hi 
Can you please share the oml file

2019-07-01 07-16-04
Vinod Patidar

Hi Maxwell,

You can get total items from aggregate count properties. You can use below forge component to get random integer between a minimal and maximal value.

https://www.outsystems.com/forge/component-overview/677/randomizer-number-generator

To get the random item from aggregates you can use list index like - GetProducts.List[0]

So whatever random number you get from the component you can use that in aggregates like - GetProducts.List[randominteger] 

Thanks

Vinod

2023-10-21 19-42-11
Tousif Khan
Champion
Solution

Hi Maxwell,

You can also achieve it through JavaScript as well you can generate a random number using random() function first you need to get the total count of the aggregate then pass it in your JavaScript through input parameter

it will return a  random number, you can use it to fetch the data from aggregate.

I have attached OML for the reference.

Sample- Link


I hope this will help

Thanks 

Tousif khan

Random_Record_Sample_Oml.oml
UserImage.jpg
Accelance Partners

Hi @Maxwell Mckinnon 


You can create SQL query and to select random Id use NEWID() 

you can take reference from stackoverflow 
https://stackoverflow.com/questions/848872/select-n-random-rows-from-sql-server-table


Thanks,
Shriyash 

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