Notifications
Announcements
No record found.
Hello Folks,
I need to Select Random Rows or Records From table using X++.
In SQL below query is working fine.
SELECT TOP 10 * FROM CustTableORDER BY NEWID();but I want to achieve the same result in D365 using X++ without any Direct SQL statement Please suggest
You can't choose random records in Ax like this (at least I couldn't remember). However, it is possible to generate a random value and compare(>= or <=) it to a field like RecId and use the first 10 records in a while loop.
You can use System.Random to generate random value.
docs.microsoft.com/.../system.random
You can identify a range by looking at the first and last RecIds.
If you would explain the business case why you would need random records it could maybe help with providing a better solution. Since D365 is using a relational database you will always get ordered data instead of random records.
simple idea;
To get some sort of random data is selecting for example the first 100 records. Put the records in an array (container) and generate a random value between 1 and 100. When for example the number is 15 get the data in the 15th element of the array.
Hi Piyush,
what's the business requirement that you're trying to solve by fetching random records?
Business Requirement -
we have to a specific number (Configurable) of records or rows from Work Orders Table for Sample Collection.
After Sample Collection , Auditor will work on those work Orders
This activity will be repeated more than 1 . So every time should pick random work orders for sample collection
***Business Requirement -
we have to Select a specific number (Configurable) of records or rows from Work Orders Table for Sample Collection.
Perhaps this is something that can be discussed with the auditor. I have not yet heard similar requirement - usually the "random" samples are picked manually and not via some random generator. I wonder why the auditor wants to let the system decide which records to pick - isn't this something that could potentially compromise the quality of the to-be-audited data?
Anyway, one simple approach would be to check lowest and highest RecId in the table, then generate random numbers between those two numbers, and fetch the corresponding record. If there's no record for the generated random RecId, pick new.
You can of course also get all RecIds and select random values from there but it would be a bit slower since the system would need to provide all existing RecId values.
But most likely you need more criteria - otherwise the auditor would most likely get records from past years and not from the year that they're auditing.
Under review
Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Martin Dráb 660 Most Valuable Professional
André Arnaud de Cal... 549 Super User 2025 Season 2
Sohaib Cheema 307 User Group Leader