Recently, while working on a project I came across a need to generate data in bulk to be inserted into Objects for testing purpose. So, it was then while searching net, I got to know about ‘Mockaroo’ which proved to be a hassle free way to generate test data as and when required using REST API callouts. The best part being one can execute it using the ‘Anonymous code window’ of Developer console which means your Org is clean as there is no trace to how the data got generated. however, if same kind of data is to be generated several times, then, we can also have a class written and call it whenever required.
In the example explained in this document, bulk data will be generated for 2 objects and then the same will be done for a junction object which has the previous 2 objects as parent.
- An account on ‘Mockaroo’. (https://mockaroo.com/users/sign_in)
All set! Lets jump into the steps to get it done:
- Using the link shown above, register on ‘Mockaroo’. Once done, an API key will get generated for your account. This key will be used in all the API calls made to mockaroo from Salesforce org. To know the API key, go to Account page after logging in to Mockaroo.
- Once ‘mockaroo’ account has been set up, login to your Salesforce Org and create 2 custom objects. (In this example, Parent One and Parent Two)
- Create another custom object in Salesforce (Junction Child) which should act as a junction object between previously created objects i.e. Parent One and Parent Two. In this example, API names of the lookup fields on ‘Junction Child’ object are Parent_One__c and Parent_Two__c.
- Go to Mockaroo account and create 2 separate schemas (each for objects Parent One and Parent Two) under the ‘Schemas’ tab. Mockaroo has it’s own set of data types (141 currently) which is needed while creating fields for the schema. Any external data type name isn’t recognized. (We can import schema details such as name of fields and data types using csv headers or JSON to prevent human error)
NOTE: For field names in schema, we must use the same name as that of the API name of fields of objects in Salesforce.
- Save the schemas.
- Create a new schema either using the headers of CSV file which contains API names of fields of Junction_Child__c object for which mock data is to be generated or manually with following steps:
- Create a field with the same name as that of the API name of the lookup field which relates Junction_Child__c to Parent_One__c.
- Create a field with the same name as that of the API name of the lookup field which relates Junction_Child__c to Parent_Two__c. (Assign any data type to the fields for now. We’ll get back to the actual data types which is to be set here later in this document)
- Save the schema
- Goto ‘APIS’ tab in mockaroo web page and click on ‘Create a New Mock API’.A page would open to configure the various parameters for the API.
- The ‘Route’ label, has 2 sections i.e. the API request methods (leave it as GET) and URL section which contains the section that gets appended after the domain part of the API endpoint.
Edit the URL section as follows:
- The ‘Handler Script’ section describes the schema to be used while creating dummy data and the number of records to be generated.
Edit these as follows:schema “Parent One”
- Click on ‘Create API’
- Once done mockaroo generates a API endpoint URL which will be used to make the callouts from Apex code.
- Repeat steps 7 to 11 for creating API for Parent Two.
- Then, goto the Developer Console and select ‘Open Execute Anonymous Window’ under ‘Debug’ menu.
- Here a REST API callout is needed get the random data that would be generated by mockaroo using the schema created in above steps. Code snippet is given below:
HttpRequest req = new HttpRequest(); //Set the request method to as ‘GET’. req.setMethod('GET'); //Set endpoint as the API endpoint shown in step 11. req.setEndpoint('https://my.api.mockaroo.com/parentone.json?key=yourMockarooAccountKey'); Http h = new Http(); HttpResponse res = new HttpResponse(); res = h.send(req); List pList = new List(); pList = (List)JSON.deserialize(res.getBody(), List.Class); insert pList;
- The result returned by mockaroo API is in JSON format. So, it needs to be deserialized using JSON.deserialize() method and casted into concerned sObject (Parent_One__c and Parent_Two__c in this case) before inserting the list into database.
- Repeat steps 13 to 15 for Parent Two.
- Extract the ‘Id’ values of the records using Workbench separately for
Parent_One__c and Parent_Two__c in csv files.
- Go to Mockaroo and click on the tab labelled ‘DATASETS’.
- Upload the csv files extracted through workbench and save those. These
will act as reference data to be populated in junction object fields.These datasets can be reused any number for times for reference as these reside at mockaroo end itself.
- Again, navigate to ‘SCHEMAS’ tab and open Junction Child schema.
- In the Type section of parent_One__c and parent_Two__c fields, select ‘Dataset Column’.
- A dropdown will show up having the names of the dataset uploaded in the previous steps. Select the respective dataset
- Another dropdown would appear which will contain the field names of the corresponding dataset. Select the ‘Id’ field.
- Save the schema.
NOTE: Datatypes used in mockaroo schemas are intrinsic to mockaroo and we cannot use any other data type.These datatypes can either be set manually or using a json file.
- Create another API for this schema by referring the steps 7 to 11.
- Go back to Developer Console and navigate to ‘Open Execute Anonymous Window’ again.
- Follow steps 13 to 15 for making a callout, but this time for Junction_Child__c. Make the necessary changes in the endpoint and deserializing section of the code.
HttpRequest req = new HttpRequest(); //Set the request method to as ‘GET’ req.setMethod('GET'); //Set endpoint as the API endpoint shown in step 11 req.setEndpoint('https://my.api.mockaroo.com/junctionchild.json?key=yourMockarooAccountKey'); Http h = new Http(); HttpResponse res = new HttpResponse(); res = h.send(req); List jList = new List(); jList = (List)JSON.deserialize(res.getBody(), List.Class); insert jList;
- Once done, navigate to the tab assigned to Junction Child object and it would contain all the dummy data along with relations to Parent_One__c and Parent_Two__c.
Limits and Considerations:
- 3 subscriptions of mockaroo are available:
>>Free (200 API calls/day)
>>Silver (no limit to number of API Calls, 1 million records/day)
>>Gold (no limit to number of API calls, 10 million records/day)
- All mockaroo response are capped at 5000 records per API call.
- Datatypes used in mockaroo schemas are intrinsic to mockaroo and any other data type cannot be used.
- Mockaroo datatypes for fields in schema can either be set manually or using a json file having required parameters.
- POST request can be done with the schema details in request body (JSON format) and mockaroo will generate random data as per that schema but this schema does not get saved at mocaroo end. Please find beow a sample code ehich is sending a simple schema to mockaroo using a POST request:
HttpRequest req = new HttpRequest(); HttpResponse res = new HttpResponse(); JSONGenerator gen = JSON.createGenerator(true); gen.writeStartObject(); gen.writeStringField('name', 'first name'); gen.writeStringField('type', 'First Name'); gen.writeEndObject(); String structure = gen.getAsString(); String URL = '<a href="https://api.mockaroo.com/api/generate.csv?key=48071d40&count=10" target="_blank" rel="noopener">https://api.mockaroo.com/api/generate.csv?key=48071d40&count=10</a>'; req.setEndpoint(URL); req.setMethod('POST'); structure= '[\n'+structure+']\n'; req.setBody(structure); Http h = new Http(); res = h.send(req); system.debug('Generated Data::'+res.getBody());
2 thoughts on “Mockaroo – An easy way to generate millions of test data”
This is a cool tutorial! I have one addition and one question:
Addition: In order to make a call out to Mockaroo, the user would need to create a remote site in Salesforce.
Question: I’m trying to create Contact records but I keep getting an error:
“Line: 15, Column: 1
System.JSONException: Unrecognized token ‘Name’: was expecting at input location [1,5]”
Below is my sample apex code. Any thoughts on what I could be doing wrong???
HttpRequest req = new HttpRequest();
//Set the request method to as ‘GET’.
//Set endpoint as the API endpoint
Http h = new Http();
HttpResponse res = new HttpResponse();
res = h.send(req);
List conList = new List();
conList = (List)JSON.deserialize(res.getBody(), List.Class);
Thanks for the suggestion.
LastName is a mandatory standard field that you need to populate while inserting a contact record. Hope this helps.