At its core software development/engineering is pure problem solving. You’re presented with a problem and you need to find an efficient and elegant way to solve the problem.

Determining a solution and implementing it is the fun part, but testing and writing unit tests can be cumbersome as it requires you to generate a significant amount of data at times. If you’re working in your own instance that you’ve built from the ground up or become familiar with over time, it’s not so bad since you know what objects need to be created and which fields are required. However, if you’re on the other side of the coin as a consultant you don’t always get a lot of time to become as familiar with an org as you’d like, so unit testing can become a pain.

Fortunately, I’ve come up with a solution!

Utilizing the metadata API’s we can generate all the SObjects we need, with all the required fields and correct data types with one simple method.

The repository can be found here:

Github: DataWizFactory

The method is called createFullObject
public static SObject createFullObject(String sObjectType, Boolean fillOnlyRequired, Boolean createReferences)

The 3 parameters are:

  • sObjectType – String of the SObject API Name you want to create
  • fillOnlyRequired – Boolean marking if you want to fill only the required fields on the object
  • createReferences – Boolean marking if you want to create parent object references (i.e. Opportunities require and Account)

So if we’re creating unit tests around a trigger on the Opportunity, we might do something like:

Opportnity testOpp = (Opportunity)DataWizFactory.createFullObject('Opportunity', True, True);

That will create an Opportunity record with an Account related to it (Assuming no custom parent object relationships exist on the Opportunity).


What type of data does it fill in for each field?

The utility looks at the metadata descriptions for each field and creates random data of the correct type for each field. So, if a field is a String with a max of 20 characters, it will create a String with a max of 20 characters.

What about picklist fields?

The metadata is also examined, and random values are selected from the options in your metadata.


More FAQ’s to come as questions arise.


Github: DataWizFactory