Skip to main content
Known Participant
Jan 19, 2022 6:41 AM UTC
Question

functions expression rules

for example, I am having 10 questions in my DB  for that each question has a unique question id

If I enter 5 then 5 question id  should be come randomly help me out on this 

    11 replies

    harshitb6843
    Brainy
    Jan 19, 2022 6:46 AM UTC

    Hi there,

    I do not understand this part. 

    should be come randomly

    But what I think is that, when you search for 5, you should get a question with id as 5. 
    For that, you use queryFilter() in query(). Documentation - https://docs.appian.com/suite/help/21.4/fnc_system_a_queryfilter.html

    If you want to combine it with any other filter, then try using logical expressions in query() function where you can pass multiple filters that can be applied all at the same time, or any of them. 

    I hope it helps. 

    Thanks,
    Harshit

    Known Participant
    Jan 19, 2022 9:19 AM UTC

    it should come randomly means if there are 10 questions I want only 5 questions that is like not comes in ordering it should be like {10,1,5,3,8}

    harshitb6843
    Brainy
    Jan 19, 2022 10:49 AM UTC

    Hi there,

    This can be another approach. You generate a list of incremental random values, all unique. And then use that array to index from the list of available questions. 

    Below is the code for the same. 

    a!localVariables(
      local!questionsData,
      local!array: 1 + tointeger(rand(5) * (1000 - 1)),
      local!sortedArray: rule!BAM_utils_sortArray(
        arrayValues_any: local!array,
        ascending_bool: true
      ),
      local!randomIndex: a!flatten(
        a!forEach(
          items: local!sortedArray,
          expression: wherecontains(fv!item, local!array)
        )
      ),
      index(
        local!questionsData,
        local!randomIndex,
        {}
      )
    )

    In local!array, I am trying to generate the values in a specific range. The parameter of rand() function will give us 5 values. You can change it to n.

    stefanhelzle0001
    Brainy
    Jan 19, 2022 7:59 AM UTC

    My quick solution is to fetch all the IDs from DB, then randomly remove items until only 5 are left.

    a!localVariables(
      local!dbIds: {3,7,42,6,9,13,123,45,889,657},
      reduce(
        rule!TST_RemoveRandomItemFromList(_,_),
        local!dbIds,
        enumerate(5)
      )
    )
    
    TST_RemoveRandomItemFromList(
      list(Integer Array),
      dummy(Integer)
      
      remove(
        ri!list,
        ceiling(rand() * count(ri!list))
      )

    stewart.burchell
    Employee
    Jan 19, 2022 9:05 AM UTC

    An alternative would be to fetch all of the questions form the DB (agnostic of their database ids), and then generate a random list of 5 integers in the range of the size of the fetched array (ensuring no duplicate random integers are generated) and then use those to index your fetched array.

    stefanhelzle0001
    Brainy
    Jan 19, 2022 9:14 AM UTC

    This may create the same value multiple times. That was my first idea as well and I had no good idea to remove duplicates. I know that union does that, but then I would have to create more than 5 random numbers. But how much? With enough bad luck, all numbers are the same ...