Hi,
First of all, I would like to apologize for my terminology. It is probably flawed.
Anyhow, I'd would like to pick a random entry from my Aggregate.
For this, I have added the RandomizerNumberGenerator.
The problem is, that I count the entries and then use this result as the max for the number generator.
This works, but it also includes entries that have been deleted. This obviously results in an error.
For example:
I have entries with the Id's 1, 2, 3, 6, 7 and 8.
The count result is 6. Id 7 and 8 get ignored and Id 4 and 5 result in an error if picked.
Is there a simple workaround for this problem?
Hi Sebastian,I would go with Pedro's solution.If however you do not want to use SQL. You can pick your record using:
YourAggregate.List[RandomizerNumberGeneratorNumber - 1]
(Don't forget the - 1 as indexes start at 0)
Hey Eric,
I know it should be easy, but I can't do it.
Can you tell me how to get the biggest Id of an Aggregate?
I have the descending sorting set, but how do I just pick the first Id?
Hi Sebastian,Did you already try Perdo's answer?For this mine you do not need the biggest Id. as you are using indexes and don't care for Ids.So the max for your randomnumber generator would be your aggregate length.Then you use the result of your random number generator as the index for my answer above
Hi Eric,
Okay, I got that, your code outputs a record.
I am stuck at the point where I don't know how to handle that record.
Do I use Assign for that or how do I extract my data from that record?
That depends on your needs. If you need the whole record you can assign it to a record.If you only need a certain value of the record you can get that specific value by continuing the dot notation and assign/use that
YourAggregate.List[RandomizerNumberGeneratorNumber - 1].valuename
IT WORKS! I can't believe it!
Your solution was so easy, if one knows how to use outsystems...
The bright side is, I can only learn from that and get better.
Thank you sooo much for your help and patience with me!
Hi Sebastian,
To retrieve only 1 random record from an entity you can use an Advanced Query with a TOP(1) and ORDER BY NEWID().
This will make sure the query returns one random record.
Hi, Sebastian Schefold
Use the condition that, when u get the random number from RandomizerNumberGenerator just check with the help of aggregate that if that random number is there in the your entity or not.
If its there then true, if not then it was not found in the aggregate.
Regards,
Faiz Khan
You could consider 2 options:
Option 1:
Option 2:
Option 1 worked for me! I didn't know about the listfilter thing.
Thank you so much!
Hi Sebastian,I would suggesting having another look at Perdo's answer as his answer is the better one. For the reason that his answer requires less data to be fetched from the database and immediately returns a random record, rather than having to fetch all the records and only then having to sort through them again.If you are already fetching all data to use elsewhere (so there already is an aggregate in place)I would still suggest to look at mine again, the reason being ids might not be sequential, there might be some missing (by deletion for example) meaning if the random number matches one of those ids and you compare the random number with the id you need to loop it multiple times
OK sure, I'll give it a go. Thank you!
I suggest you reference the RandomChoice client action from the Client Utilities Forge Component.
You simply have to pass your list of Ids as the input parameter for the action, and you'll get a random element from that list as an output parameter.
You can find the Forge Component here:
https://www.outsystems.com/forge/component-overview/12227/client-utilities
The main Demo here:
https://paulojmoreira.outsystemscloud.com/ClientUtilitiesDemo/Home
And the RandomChoice client action demo here:
https://paulojmoreira.outsystemscloud.com/ClientUtilitiesDemo/Random
Hope this helps.
Best regards,
Paulo Moreira