0

I have this on my controller.

private String userName;
userList = [SELECT Username, Question_1__c, Question_2__c, Question_3__c 
FROM User WHERE Username = :userName LIMIT 1];

What should I do to display 2 out of 3 question randomly on an output panel?

<apex:outputField value="{!userList[0].QUESTIONHERE123}"/>
<apex:outputField value="{!userList[0].QUESTIONHERE123}"/>

1 Answer 1

1

I would use the random funtion in the Math class and create a method:

public Integer randomWithLimit(Integer upperLimit){
   Integer rand = Math.round(Math.random() * 10);
   return Math.mod(rand,upperLimit);
}

I multiply it by 10 because I want a range between 0 and 9. Since you always have 3 questions I would have the upper limit to 3.

Also why do you have different fields for different questions? The rest of the code I imagine to be; you select all the questions from the database and store them in a list:

List<Questions__c> questions = [Select Question__c from Questions__c];
Integer removeAt = randomWithLimit(questions.size());
questions.remove(removeAt);
//Remove the 2 questions to the visualforce page
return questions;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .