SFDC Randomizer

Who needs predictable? Sometimes we need to get a little random. Here is a utility class you can put into any Salesforce org to generate all sorts of random things such as a random number, a random string, even a random sobject ID from a list.


Let’s get random here for a moment.

Have you ever needed a random number in Salesforce.com? Or maybe you need to pick a random value from a list or have the system return a random Boolean value. Here is the solution, the SFDC Randomizer class. Install this randomizer class in your org and call the available methods from any other APEX class or trigger.

This class is all built off of the getRandomNumber method which can be called to generate a random integer, but also provides a “random seed” for the other methods in the class. So if you choose to only copy individual methods to an existing class, be sure to understand which other methods will be called and copy those as well.

  Download the Randomizer class

The table below shows the available methods, their arguments and what they return:

Name Arguments Return Type Description
getRandomNumber Integer size The size integer represents the maximum value that can be returned.
For example:
getRandomNumber(10) could return any number between 0 and 9. getRandomNumber(1000) could return any umber between 0 and 999.
getRandomBoolean Boolean Randomly returns either true or false.
getRandomString List strings String Returns a random item from the list of strings. A good use for this is to select a random object by passing a list of Salesforce ID string.
getRandomPickListValue SObjects object
Stringfield name
Boolean allow_blank
String Returns a random picklist value for a given object and field name. This good use for this is for randomizing values when creating “dummy” objects in a new sandbox or for testing.


  This method uses a describe call within a loop. At the time of writing this post there is a governor limit of 100 describe calls per execution. This will cause an error if the object has over 100 field, or if you call it from a loop. If you need to loop through a number of records, consider calling the getPicVals method outside your loop, then use getRandomString to select as random value.
getPicVals SObjects object Map<String,List<String>> Returns a map of all picklist/multi-picklist fields and their values for a given object. The key for each map entry is the name of the field and the value is a list of strings representing the picklist values.


  The field names in the keyset are in proper case (My_Custom_Field__c rather than my_custom_field__c). Keep this in mind when you attempt to get a value from the map.
getPlaceholderText Integer length String Returns placeholder (Lorem Ipsum) text of a certain length. This is not a full-fledged Lorem Ipsum generator, but it should do the trick for most cases.
More about Lorem Ipsum
  in an effort to maintain proper sentence structure without adding too much logic, it is possible for the returned string to be up to two characters shorter than the length argument, but never more.

Usage and Examples

Example 1 – Generating a random number

Once you have the randomizer class in your org, you can generate a random number using the following code.

The argument for the getRandomNumber method represents the number of possible results. So if you entered 10, the possible results could be from 0-9. If you entered 1000, the possible results could be from 0-999.

Example 2 – Update a record with a random picklist value

For a single record, you can use the getRandomPicklistValue. However, this should not be called from a loop because the method uses describe calls and there are only 100 describe calls allowed in a single execution. See example 3 below on how to use the getPickVals method to get around this governor limit.

Example 3 – Populating a config sandbox with randomized sample data

So let’s say you just created a new config or developer sandbox and yo need to load in some “dummy” records for testing your new development. You can create more realistic data by randomizing the field values. Here, we can create a number of Accounts with randomized values.

Example 4 – Get random cases for a quality audit

Here we will create a visualforce page that a manager can use to pull 10 random cases created today for a quality control audit.

Visualforce Page

APEX Controller

Thanks so much for reading. I hope you found this post to be helpful. If you did, I would appreciate a comment or two. They are the fuel that give me the energy to keep posting. Also , let me know if you had any problems getting this solution to work for you and I will do my best to get back with you as soon as I can.


Leave a Reply

Legacy Comments - Closed

  1. James Dougherty says:

    Thanks! I'm very interested in the Example 4 – Get random cases for a quality audit

    We actually pick for Quality audit by two concepts –
    1) Complex cases always require quality review
    2) setup percentages for new users (75% of cases reviewed in first year; 50% after; etc)

    Hoping to employ this concept in a solution for those.

  2. Santosh says:

    Need your help:
    Using 'Mass Email Lead' option for sending Email to particular Lead

    Need help to track the click on the link (to Website, inside the email) by the Email receiver (only the count of Click on the link to be tracked in Salesforce Account)

    Please Help

    Warm Regards

  3. shantinath says:

    very helpful code.. thanks 🙂

  4. So I've been doing a lot of work lately that involves making SFDC not look like SFDC so the above example renders out to a simple HTML table. If you want to keep with the SFDC design patern, use the code below on your VP page.

    <apex:page controller="randomTesterController">
    <style type="text/css">
    <apex:sectionHeader title="Cases for Audit"/>
    <apex:pageBlock title="Random Cases">
    <apex:pageBlockTable value="{!RandomCases}" var="c">
    <apex:column headerValue="Case Number">
    <apex:outputLink value="{c.Id}">{!c.CaseNumber}</apex:outputLink>
    <apex:column headerValue="Case ID">
    <apex:outputLink value="{c.Id}">{!c.Id}</apex:outputLink>
    <apex:column headerValue="Case Owner" value="{!c.Owner.Name}" />

  5. Sorry, forgot to HTML encode the VP page code so that this page does not render the HTML.

    This is what the VP Page should look like

    <apex:page controller="randomTesterController">
    <th>Case Number</th><th>Case ID</th><th>Case Owner</th>
    <apex:repeat value="{!RandomCases}" var="c">
    <td><a target="_blank" href="/{!c.Id}">{!c.CaseNumber}</a></td>
    <td><a target="_blank" href="/{!c.Id}">{!c.Id}</a></td>

  6. Sure, you might try something like this:

    VF Page:

    Case NumberCase IDCase Owner


    public class randomTesterController {

    public List RandomCases {get;set;}

    public randomTesterController(){

    //Number of random cases to be returned
    Integer numberOfCases = 10;

    //Make sure the list is not null and is empty
    RandomCases = new List();

    //Query the cases, modify the where clause to fit your needs
    List cases = [select Id, CaseNumber, Owner.Name from case where createddate = today];

    //Make sure there are enough records
    if(cases.size() >= numberOfCases){

    //create a list of strings to pass to our randomizer method
    List caseIDs = new List();
    Map caseMap = new Map();
    for(Case c : cases){

    //create a set to hold the returned ids so that we can make sure there are no dupliates
    Set usedIds = new Set();

    //Now lets get the random Case IDs
    for(Integer i=0; i String randomId = randomizer.getRandomString(caseIDs);
    //check for duplicates
    randomId = randomizer.getRandomString(caseIDs);
    //TODO:handle exception

    This is just a quick and dirty example. I really didn't test it, but this should get you going.

  7. Joe says:

    I was wondering if you can point me in the right direction on how I can have the CaseNumber and CaseOwner list in the visualforce page so it looks something like this:

    1xx000000dwerrhb23 00034577 John Smith

  8. Tom says:

    nice post and solid examples. Thanx for sharing this. Among other things this should make writing test classes faster for the rest of us.

  9. Max says:

    thanks dude this really helped me out :thumbsup:

  10. Lexi says:

    Perfect! Just what I was looking for. Thanks for Sharing!

Please use the Google+ Comment section above. Thanks!

Advertisment ad adsense adlogger