Writing the Apex code and tests in an integration using REST

Integrating third party apps with your Salesforce app can be done easily using the JSON2Apex tool. We provide to you here the method by which you can generate apex types, test methods and mock service classes for REST integration callouts.

The method illustrated here utilizes thehttps://json2apex.herokuapp.com/ tool to generate the apex code units, the JSON format is a arbitrary string used to generate types.

This approach allows the user to de-serialize the response directly without having to iterate manually which could be tedious, error prone and time consuming

Here are the step-by-step instructions on implementing the said solution –

Step 1

Copy the expected response JSON into https://json2apex.herokuapp.com/ page and click on “create Apex” button which generates the apex code for you. For example, JSON input to https://json2apex.herokuapp.com/:


The following would be the generated apex code.




Step 2

The first part of the Code has all the types (inner classes) corresponding the structure and input JSON string which will be used to de-serialize the JSON string. Also, the last method within the generated code is the test method just returning the JSON formatted input which can be used as for a mock callout for test methods to get the code coverage for the organization.


This approach is very neat and easy to implement without a lot of effort and zero debugging. Thanks to https://json2apex.herokuapp.com/ for providing such a effective tool and making the developer’s life a lot easier.