Skip to main content

Alternative Approach: Creating a Utility Method

Alternative Approach: Creating a Utility Method

There is another way to use the methods of the %PopulateOpens in a new tab and %PopulateUtilsOpens in a new tab classes. Rather than using %PopulateOpens in a new tab as a superclass, write a utility method that generates data for your classes.

In this code, for each class, iterate a desired number of times. In each iteration:

  1. Create a new object.

  2. Set each property using a suitable random (or nearly random) value.

    To generate data for a property, call a method of %PopulateOpens in a new tab or %PopulateUtilsOpens in a new tab or use your own method.

  3. Save the object.

As with the standard approach, it is necessary to generate data for independent classes before generating it for the dependent classes.

Tips for Building Structure into the Data

In some cases, you might want to include certain values for only a percentage of the cases. You can use the $RANDOM function to do this. For example, use this function to define a method that returns true or false randomly, depending on a cutoff percentage that you provide as an argument. So, for example, it can return true 10% of the time or 75% of the time.

When you generate data for a property, you can use this method to determine whether or not to assign a value:

 If ..RandomTrue(15) {
    set ..property="something"
 } 

In the example shown here, approximately 15 percent of the records will have the given value for this property.

In other cases, you might need to simulate a distribution. To do so, set up and use a lottery system. For example, suppose that 1/4 of the values should be A, 1/4 of the values should be B, and 1/2 the values should be C. The logic for the lottery can go like this:

  1. Choose an integer from 1 to 100, inclusive.

  2. If the number is less than 25, return value A.

  3. If the number is between 25 and 49, inclusive, return value B.

  4. Otherwise, return value C.

FeedbackOpens in a new tab