Mockaroo – An easy way to generate millions of test data

Mockaroo - An easy way to generate millions of test data

Recently, while working on a project I came across a need to generate data in bulk to be inserted into Objects https://mockaroo.com/sign_in 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.

Prerequisites:

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.

account_page

  1. 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)
  2. 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.
  3. 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.
    Schema
  4.  List of schemas available in a mockaroo account Schema
  5. Schema Builder page Data Data types available in Mockaroo
  6. Save the schemas.
  7. 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
  8. 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.
  9. 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:
    /ParentOne.json
  10. 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”generate 10 mock_API
  11. see description of each parameter (schema and generate) on the right side section 
  12. Click on ‘Create API’
  13. Once done mockaroo generates a API endpoint URL which will be used to make the callouts from Apex code.
  14. mock_API_2
  15. Endpoint generated
  16. Repeat steps 7 to 11 for creating API for Parent Two.
  17. Then, goto the Developer Console and select ‘Open Execute Anonymous Window’ under ‘Debug’ menu.
  18. 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:[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/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;
    [/code]
  19. 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.
  20. Repeat steps 13 to 15 for Parent Two.
  21. Extract the ‘Id’ values of the records using Workbench separately for
    Parent_One__c and Parent_Two__c in csv files.
  22. Go to Mockaroo and click on the tab labelled ‘DATASETS’.
  23. 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.1
  24. Again, navigate to ‘SCHEMAS’ tab and open Junction Child schema.
  25. In the Type section of parent_One__c and parent_Two__c fields, select ‘Dataset Column’.
  26. A dropdown will show up having the names of the dataset uploaded in the previous steps. Select the respective dataset
  27. Another dropdown would appear which will contain the field names of the corresponding dataset. Select the ‘Id’ field.
  28. 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.
  29. Create another API for this schema by referring the steps 7 to 11.
  30. Go back to Developer Console and navigate to ‘Open Execute Anonymous Window’ again.
  31. 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.[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;
    [/code]
  32. 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.2

Limits and Considerations:

  1. 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)
  2. All mockaroo response are capped at 5000 records per API call.
  3. Datatypes used in mockaroo schemas are intrinsic to mockaroo and any other data type cannot be used.
  4. Mockaroo datatypes for fields in schema can either be set manually or using a json file having required parameters.
  5. 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:

    [code]
    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 = ‘https://api.mockaroo.com/api/generate.csv?key=48071d40&count=10‘;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());
    [/code]

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

Salesforce CRM Insights
From Data to Action: Tableau Agent’s Role in Shaping Salesforce CRM Insights
future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top