53
Views
13
Comments
Solved
Deleted Id in Aggregate
Application Type
Traditional Web
Service Studio Version
11.14.2 (Build 56067)

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?


2018-10-18 08-34-00
Eric Bulters
 
MVP
Solution

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)


UserImage.jpg
Basti1399

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?


2018-10-18 08-34-00
Eric Bulters
 
MVP

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

UserImage.jpg
Basti1399

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?


2018-10-18 08-34-00
Eric Bulters
 
MVP

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
UserImage.jpg
Basti1399

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!

2024-11-20 08-28-20
Pedro Marques

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.

2022-01-05 09-37-24
Faiz Khan

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

2018-10-18 08-34-00
Eric Bulters
 
MVP
Solution

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)


UserImage.jpg
Basti1399

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?


2018-10-18 08-34-00
Eric Bulters
 
MVP

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

UserImage.jpg
Basti1399

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?


2018-10-18 08-34-00
Eric Bulters
 
MVP

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
UserImage.jpg
Basti1399

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!

2020-09-01 10-42-42
Stefano Valente

You could consider 2 options:

Option 1:

  1. Get id's with an aggregate, order by id desc.
  2. the first record is the max id
  3. get randomnumber, limit to max id
  4. listfilter the aggregate with the randomnumber.
  5. if listfilter empty, back to 3, if not empty return.


Option 2:

  1. Get records with an aggregate. Order how you like
  2. get randmomnumber
  3. go to the listnumber of your randomnumber (if list < randomnumber, remove listlength until it <= list)


UserImage.jpg
Basti1399

Option 1 worked for me! I didn't know about the listfilter thing.  

Thank you so much!

2018-10-18 08-34-00
Eric Bulters
 
MVP

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

UserImage.jpg
Basti1399

OK sure, I'll give it a go. Thank you!

2024-06-13 07-53-34
Paulo Moreira
 
MVP

Hi Sebastian,

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

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