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
preddypreddy 

Random Record retrieving for auditing

Hi All,

 

i have an requirement like i need to fetch the 10 % records randomly for auditing.for example in case object i have 400 records,from those records i need to fetch 40(10 % of 400) records  randomly and i need to display those records in List view which is to be displayed for auditing.

 

if u have done this type of requirement previously please share the ideas.

 

Appreciate your suggestions.

Satish_SFDCSatish_SFDC
No direct way i can think of.
But you can select all 400 records and then out of the list you can randomly select 40 records using the random() method.

Regards,
Satish Kumar
Satish_SFDCSatish_SFDC
Or if you have an autonumber field in your object, you can use the Math.random() class to generate some random numbers and then issue an SOQL query to fetch records matching these random numbers with the autonumber field.

Regards,
Satish Kumar
preddypreddy

Thanks for ur quick reply Sathish.

The thing is I want to display 10 % of records from database for every type.type is a picklist field which is having more than 10 picklist values.for each type value I need to fetch 10 % of records randomly.

Regards,
praveen Reddy. R


preddypreddy

I have tried to generate random number and then I took random number as offset in soql query.it is giving records from that offset.

Please find the below code I have written

public with sharing class autorun {

public List cases = new List();
public List casessize = new List();

public autorun(ApexPages.StandardController controller) {
}
public list caselist=new list();
public List getcases(){
integer count=[select count() from case];
integer upperLimit=count;
set selected=new set();
for (Integer i =0; i< 10; i++){
Integer rand = Math.round(Math.random()*count);
Integer numbers =Math.mod(rand, upperLimit);
selected.add(numbers);
system.debug('rand'+rand);
system.debug('numbers'+numbers);
system.debug('selcted'+selected);
cases=[select id from case limit 1 OFFSET :numbers];
system.debug('cases'+cases);
//caselist.add(cases);

}

return cases;

}
}

Regards,
praveen Reddy. R


Satish_SFDCSatish_SFDC
As per my understanding of your post above:
There are a lot of records with different picklist values in your object.
We need to count the number of records for each picklist value separately, and then fetch 10% records of the individual counts.Also we have to repeat this for all the 10 different picklist values.

If this is the case i have a question:
How often do you have to do this task of fetching random records. Can we assume that in the next round of auditing, the records created before the previous auditing date be ignored.
In that case, we will have a smaller set of fresh records, so we can get all of them and do our processing after getting the records.

Regards,
Satish Kumar
preddypreddy

once the case is went for audit..auditor will update the audit fields(score,comeents lke that) so that audit has completed for that case.we need to care about that one also while fetching the records.

Satish_SFDCSatish_SFDC
Say if an auditing is done today (16th October) and the next auditing will be done on 16th November.
In the 2nd round of auditing (16th November), is it enough get just the records created between 16 Oct and 16 Nov.

If that is the case, create an SOQL query to get all the fresh records created after the last audit date.
Once you have the list, you can then loop through them and put them in different lists based on the picklist value. All records for one picklist value will be in a single list. From each such list, you can pull random records by using the Math.Random() to generate an integer and get the record at the specifc index. Make sure you your generated integer is less than the size of the list.

Regards,
Satish Kumar