4

I'm trying to create a custom field that takes today's date and adds a random number of days to it and returns that new date? Any ideas?

2 Answers 2

4

As of yet, there is no way to generate a random number using just formula functions (that I know of without hitting the compiled character limit).

An alternative is to make it a standard date field and create a new trigger on the object:

trigger <name> on <object> (before insert) {

public integer MAX_RANDOM_VAL = 10;

for (<object> s : trigger.new) {
    integer daysToAdd = integer.ValueOf(Math.rint(Math.random() * MAX_RANDOM_VAL));
    s.RandomDate__c = Date.today().addDays(daysToAdd);
}
1

An alternative solution, if you are not able to (or don't want to) add a trigger, is to generate a pseudo random number using an auto number formula + a last modified date value converted to a value all modded together.

This is not really random, but it's random enough for generating arbitrary dates.

Your formula would look something like this:

TODAY() + 
MOD(
 VALUE(Unique_Number__c) + 
 VALUE(
 SUBSTITUTE( 
 SUBSTITUTE( 
 SUBSTITUTE( 
 SUBSTITUTE(TEXT(DateTimeValue(CreatedDate)), ":", ""), "-", ""), " ", ""), "Z", "")
 )
,30)/3

You'll end up with a list of dates like this (I've added the value generated as an extra column to show you the randomish number that gets generated)

Account list

Note You'll have to add an an Auto Number field - Mine is called Unique_Number__c - it is just a straight number field with no formatting.

You must log in to answer this question.

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