Pages

Friday, March 10, 2017

Generating random number between two numbers and finding string value from list of values randomly

As we know in apex, math.random() function gives random number greater than or equal to zero and less than 1. This function can be used to generate random number in apex.

I have created a apex class which can be used to find a random between 2 numbers and this can be extended later to find random string from list of strings values or to select random picklist value for picklist field while creating test data in test class.

Below is apex class code:


In order to find random picklist value, use apex describe to store all picklist values in List and then pass that list to "findRandomStringFromList" method in RandomUtility apex class.


Hope this will help!!!


More Blogs>>: 
DYNAMIC APEX IN SALESFORCE
FETCHING FILE FROM EXTERNAL/PUBLIC URL AND STORING IN SALESFORCE
SOQL INJECTION IN SOQL
CUSTOM METADATA AND CUSTOM SETTINGS IMPLEMENTATION TRICKS
SMART TABLE USING ANGULARJS IN VISUALFORCE PAGE
VISUALFORCE COMPONENT FOR RECORD STATUS BAR

4 comments:

  1. Thanks for this. I just installed into one of my test environments, and I think it's really cool. I'm just getting started with apex, so still trying to learn how some of this stuff works. The one snag I'm hitting is that, when placing this class inside of a Flow (which is being called by Process Builder), it breaks when updating more than one record. It works fine when updating a single record, but when I load many records, or update multiple in a List View, I receive an error that says "the number of results does not match the number of interviews that were executed in a single bulk execution request". Does this mean the code must be tweaked? Any help would be appreciated!

    ReplyDelete
    Replies
    1. Did you ever get this figured out? I am having the same issue. Oddly, while it causes alerts and errors, it isn't preventing record creation and generates the random numbers just fine - I am not seeing duplicated numbers in the sets.

      Delete
  2. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. The Random Blogger

    ReplyDelete
  3. Building an Access information base typically begins with chipping away at the tables first and getting the right sorts of informational index in your application from the beginning saving you a ton of time and cerebral pains later when computing and alluding to numeric qualities somewhere else.information about numbers

    ReplyDelete