function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leena.devLeena.dev 

previous button

Iam getting records "RANDOMLY" to vf page. now How to create a previous button.which gives the same previous questions when i click on previous button?

goabhigogoabhigo

Can you explain further? Code snippet would be helpful, also some light on functinality it will help.

Leena.devLeena.dev

Hi Abi,,,am working on Online exam development project,i have thousands of questions in back end,am getting questions randomly to vf page,but my problem is how to show the exact previous question when user clicks on previous button?? 

 

i have used the following formula,,but questions are repeating..i want the question not to repeat..

 

Integer rand = Math.floor(Math.random()*18).intValue();

quess = new list<Questions__c>();
if(!i.contains(rand)){
quess = [SELECT name,Answer_1__c,Answer_2__c,Answer_3__c,Answer_4__c,Answer_5__c,Answer_6__c FROM Questions__c limit 1 OFFSET :rand];
}

Shailesh DeshpandeShailesh Deshpande
You need to use the order by clause in you query. With this all your results will be ordered and you will be taken to exactly the same set of questions when you click next/previous.

However if your requirement is to have random questions on each page, then you will need to maintain a map of page numbers and the questions that appear. MAP<PAGEnumber, List<questions>>. And then based on the pagenumber u r you will need to return the corresponding list. Everytime you click the next/previous button, pass the pagenumber in the class and first check within your map if you have the page number available in it. If yes, return the list else claculate the offset and return the results.
Shailesh DeshpandeShailesh Deshpande
Oops. Just noticed you are limiting the no. Of records to 1. In that case you just need to maintain a Map<pagenumber, qquestion>. And flow the same method which I explained in the earlier post.
goabhigogoabhigo

You can definitely try out what Shailesh suggested. But when I saw your reply the thing flashed in my mind:

 

  • Insert the IDs of records that are shown already into a set - Set<Id> queriedIds = new Set<Id> ();
  • In your query, exclude this set - WHERE Id NOT IN: queriedIds];

Can you try this and let me know if this works?