Random Retrieval of Data

Hello Community, Is there a way to randomly retrieve 1 data from the database. which is not bounded by any specific conditions or in any order. Complete random.   Thanks in advance.    
1 answers

Hi Abhishek,

You can do this by using a microflow and a java action,

First, retrieve all the objects of an entity and then

You can use the Math.random() JavaScript function in a Java action to generate a random number and sort the list based on this number.

Here are the general steps to do this:

  1. Create a Java action in your Mendix module.
  2. Add a parameter of type List<yourEntity> to the Java action.
  3. Use the following code in the Java action to shuffle the list based on a random number:
    import java.util.*;
    public class YourJavaActionName {
        public static List<yourEntity> shuffleList(List<yourEntity> inputList) {
            List<yourEntity> list = new ArrayList<>(inputList);
            Collections.shuffle(list, new Random(System.currentTimeMillis()));
            return list;


  4. In your microflow, use a 'Java Action' activity to call the Java action and pass the list of objects you want to shuffle as a parameter.
  5. Use a 'List Operation' activity to retrieve the first object in the shuffled list. You can use the 'Head' function to do this.

Hope it helps!!