ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Mockaroo – An easy way to generate millions of test data

Home / Article & Blogs / Apex / Mockaroo – An easy way to generate millions of test data
By Pratik Kumar inApex

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.

Prerequisites:

  • An account on ‘Mockaroo’. (https://mockaroo.com/users/sign_in)

 

All set! Lets jump into the steps to get it done:

  1. 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
  2. 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)
  3. 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.
  4. 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 Page
    List of schemas available in a mockaroo account

    Schema Builder page
    Schema Builder page

    Data Types
    Data types available in Mockaroo

     

  5. Save the schemas.
  6. 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
  7. 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.
  8. 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
  9. 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
    see description of each parameter (schema and generate) on the right side section

     

     

  10. Click on ‘Create API’
  11. Once done mockaroo generates a API endpoint URL which will be used to make the callouts from Apex code.
    mock_API_2
    Endpoint generated

     

  12. Repeat steps 7 to 11 for creating API for Parent Two.
  13. Then, goto the Developer Console and select ‘Open Execute Anonymous Window’ under ‘Debug’ menu.
  14. 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]
  15. 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.
  16. Repeat steps 13 to 15 for Parent Two.
  17. Extract the ‘Id’ values of the records using Workbench separately for
    Parent_One__c and Parent_Two__c in csv files.
  18. Go to Mockaroo and click on the tab labelled ‘DATASETS’.
  19. 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
  20. Again, navigate to ‘SCHEMAS’ tab and open Junction Child schema.
  21. In the Type section of parent_One__c and parent_Two__c fields, select ‘Dataset Column’.
  22. A dropdown will show up having the names of the dataset uploaded in the previous steps. Select the respective dataset
  23. Another dropdown would appear which will contain the field names of the corresponding dataset. Select the ‘Id’ field.
  24. 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.
  25. Create another API for this schema by referring the steps 7 to 11.
  26. Go back to Developer Console and navigate to ‘Open Execute Anonymous Window’ again.
  27. 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]
  28. 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]

bulk test datamockarooRESTRestfulSalesforceSalesforce.comtest data
102
Like this post
1 Post
Pratik Kumar

Search Posts

Archives

Categories

Recent posts

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

What Is Graphic Design? (Executive Summary 2023)

What Is Graphic Design? (Executive Summary 2023)

  • Previous PostLightning Components As Flow Screens
  • Next PostSalesforce Integration with LinkedIn

Related Posts

Integrate SharePoint with Salesforce using Microsoft Graph API
Apex Integration Salesforce

Integrate SharePoint with Salesforce using Microsoft Graph API

Cyber-security in an uncertain world
Apex

Cyber-security in an uncertain world

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

1 Comment

  1. Jesse Twum-Boafo
    Reply
    26 September 2018

    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’.
    req.setMethod(‘GET’);

    //Set endpoint as the API endpoint
    req.setEndpoint(‘https://my.api.mockaroo.com/Contacts.json?key=XXXXXXXX’);

    Http h = new Http();
    HttpResponse res = new HttpResponse();
    res = h.send(req);
    List conList = new List();
    conList = (List)JSON.deserialize(res.getBody(), List.Class);
    insert conList;

    Reply

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo

INDIA | USA | UAE

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT