Skip to main content

Salesforce - Free Live Callout Mock Web Service Testing Using a JSON Bin or JSON Hosting Service

A great developer crafts elegant solutions, while a true pro delivers on schedule, every time!

Guys, let's enhance the testing techniques in Salesforce callouts, we have so many options available there just to be utilized. So let's deep dive to make your development more robust and timeline-friendly than ever!

Many a time it happens that your 3rd party environment or web service is not ready and you end up developing the piece from your end to GET, POST, DELETE or PUT callouts. Now as the 3rd party services are not active to support your callouts, you just have a JSON format or an example JSON. You have many ways from which you may still be able to test and make sure your development is working as expected and you may be able to fix any bugs detected and keep up with the timeline.

Things you will need before you consider taking this approach.

  • Your side of callout development is partially/fully complete.
  • You are aware of the HTTP method to be used.
  • You have the JSON architecture or an example JSON.
If you have the JSON example, you may use it directly by mocking the values or fields a bit as they might have some business-sensitive data.

Now, if you only know the format to be used in the JSON you will be interacting with, make an example JSON yourself. It is easy and barely like a wrapper class.
  1. Open a notepad.
  2. See below and understand, the things directly inside the curly brackets are a single entity and any square brackets work as a list of entities. Create your JSON following this convention. Note - Don't leave extra white spaces to save yourself from troubles
  3. [ { "Name" : "noni1", "Description" : "Noni1 Company", "Account Number" : "1ABC1234" }, { "Name" : "noni2", "Description" : "Noni2 Company", "Account Number" : "2ABC1234" } ]
  4. Copy your created JSON lines from the notepad.
Now you will need to find a JSONBin or JSON hosting service, this typically provides
you to perform GET, POST, or any HTTP method you want to use. These JSON bins can be
used by your other team members or anyone you would like, they don't even have to create
an account or something.

I prefer https://extendsclass.com. As it is simple and free.

  1. Go to the JSON hosting service, you might have to create an account, but it can be done from your personal mail.
  2. Find an option to create a JSON bin and paste your JSON data to create your JSON bin.
  3. The website will return with the address of your JSON Bin, save it somewhere, this is the callout URI that you will be using.
  4. It will also return with another URL where later on you may go and make changes to your JSON file. 
  5. The number of JSON you may upload in a day may or may not be limited.

Now go to your Salesforce setup and create a Remote Site Setting. 
  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter a descriptive term for the Remote Site Name.
  4. and paste the URL of your JSON bin for the remote site.
  5. Optionally, enter a description of the site.
  6. Click Save.
Now, go to your apex class from where you are making this callout. In the endpoint
parameter, paste the JSON bin URL. See the example below.

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('<Paste JSON bin URL>');
request.setMethod('GET'); //your required method
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
system.debug(response.getBody());
<Your business logic here>
}

Hope it helps!

If you have any doubts, comment here, I will try to help.

Comments

Popular posts from this blog

Integrate Salesforce to ChatGPT

Connecting ChatGPT with Salesforce You need to follow below steps in order to connect ChatGPT to salesforce. Create and configure your OpenAI account. Get OpenAI API keys to integrate. Setup Salesforce to be able to connect to ChatGPT. Write custom code to callout to ChatGPT and get response.                  1.              Create and configure your OpenAI account. Go to https://openai.com/ and setup your account. Click on Log in and it will take you to a page where you can sign up using various methods like google or your email. Once you have logged in into OpenAI, you will see below page. Please select API . Optionally, if you do not have idea or need knowledge on how ChatGPT works, please go through the documentations and references. 2.             Get OpenAI API keys to integrate. ...

Salesforce - Enable guest user on your digital experience site or community

Learnings -  ๐Ÿ‘‰ How guest user profiles work in Salesforce ๐Ÿ‘‰ Configuring a guest user profile ๐Ÿ‘‰Managing access and best practices Guest users in Salesforce Communities/Sites  Guest user profiles are used for controlling the public or unauthenticated access to your org's data i.e. it controls the access you give to the users who have not logged into your community.  Guest users can always see login and login error pages in your site.  Every time a portal or site is created in your Salesforce org, a Guest User Profile is automatically created at the backend. These auto-created profiles are very restrictive and do not allow any data or object visibility to guest users. Each community or site has a guest user associated with it. If you have 10 communities in your org, you will have 10 different guest users. Configuring guest user profiles in Salesforce Guest user profiles are not visible in the Setup>Profile section. Follow the below steps to go to the Guest user pr...

Salesforce LWC - Uncommon and Difficult, Tough, Tricky Interview Questions Answers | LWC interview: Advance Level

  How is LWC different from the Lightning Aura framework? Which is better to use? Aura components are upgraded to LWC to better utilize modern JavaScript and Web Stack. It was not possible for Salesforce to come with LWC before as the Web Stack would not support it. So, it is an upgraded version of the Aura component, hence it is always better in every way. However, when you develop Lightning web components, you also may need to use Aura, because LWC doesn’t yet support everything that Aura does. Always choose Lightning Web Components unless you need a feature that isn’t supported. Lightning web components are custom HTML elements built using HTML and modern JavaScript. Lightning Web Components uses core Web Components standards and runs natively in browsers, therefore, Lightning Web Components is lightweight and delivers exceptional performance. Aura components can be integrated within LWC, but it is not the same vice versa. Utilizing modern JavaScript makes it easier to be adopte...