SFDC Stop - Always the latest about Salesforce

Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.

Telegram logo   Join our Telegram Channel

Tuesday 5 November 2019

Salesforce Integration Tutorial Part 3 - Exploring POST Method

Hello Trailblazers,

This is the third tutorial in the Salesforce Integraton Tutorial Series. In the previous tutorial, we explored the GET method, created two APIs and tested them using workbench. You can have a look at the same by clicking here. In this tutorial, we will explore the POST method in detail. Throughout the whole series, we're going to work with the predefined Contact object and will be performing operations on this using the custom REST APIs.

The POST method is mainly used to create new data and has an annotation of @HTTPPost. In our case, we're going to create a contact by defining a new method in our ContactResource class by the name of createNewContact. Let's have a look at the below code:-

As you can see above, in place of GET method, I have a POST method called createNewContact in my ContactResource class i.e. a method with @HTTPPost annotation. This method has a return type of Contact as we're going to return newly created Contact in the response for confirmation. This method is taking four parameters in the input:- firstName, lastName, birthDate and leadSource.

Inside this method, I simply created a new instance of the Contact object. Then I assigned the contact's FirstName, LastName, Birthdate and LeadSource, their respective values using my input parameters. As the birthdate is of date type, so, I parsed it using the parse() method of predefined Date class. Finally, I inserted the contact and returned it in the response.

If you test this API from workbench, you'll see the below output:-

As you can see above, I have selected the method as POST among the radio buttons and in the request body, I have added the below JSON:-

  "firstName": "Richard",
  "lastName": "Hendricks",
  "birthDate": "08/03/1989",
  "leadSource": "Web"

The 4 keys in the JSON object above represent the 4 parameters of my createNewContact method. Therefore, the values that I pass in here are automatically mapped to the respective parameters using the keys. Make sure that the keys have the same name as the name of the parameter in your method.

In the response, you can see that I have a JSON object which consists of all the parameters I've sent along with the record Id. Using this, I can verify that my record is inserted successfully. I can also have a look at the raw response which is as given below:-

On having a closer look at the response, you can see that the response JSON object consists of keys which are nothing but the actual API names of fields in the contact object. It is showing me the contact record which is created in Salesforce along with the id of the record. You can use the id to check the record in your org and you'll see that the record is created successfully.

Congratulations..!! You've created a record using your own custom API in Salesforce. Now the question arises:-

What is the maximum number of parameters that I can add to a POST method ?

We've added 4 parameters currently to our post method which are used to create a new record. But, what if I have to create a record which have 50 fields ? In that case, I need to pass the value of all the 50 fields to the API. Should I create a method with 50 parameters ? Is that allowed ?

Well, I have tried that thing long ago and I didn't remember exactly but I think Salesforce will not allow you to create a method with more than 15-20 parameters. So, we need to think of a different solution. Now, we know that all the values are coming to our API in JSON format through our request body. This means that, if I can access the request body, I can extract the value of each field from the request body itself. Let's have a look at the below code of a different class named ContactResource1 which have a URL mapping of /ContactAPI1/*:-

If you see above, I have initialized the instance of RestRequest class which is contactRequest. Then I got the requestbody from the request by using the requestBody variable of RestRequest class. I have converted the request body to string using toString() method as the request body is originally in BLOB format and assigned it to a String called requestBody. Then, as we know that the request body is in the form of JSON, so, I deserialized the JSON request string into an object using JSON.deserialize(). JSON.deserialize() method takes the JSON string in the first parameter and the wrapper class in the second parameter. In this case my wrapper class is the predefined Contact.class of salesforce. It returns an object so I have to cast it to an instance of Contact using (Contact) before the JSON.deserialize() method. Finally, I have the contact object that I am inserting and returning in the response.

I have tested this API in workbench and got the below result:-

As you see above, I can add as many fields as I want in the request body and I got the contact created in my org with all those fields filled with the data provided. While working by this method, you need to take care of some things like:- the date format should be like:- YYYY-MM-DD in case of date field so that it can be easily parsed, the boolean field should have a value as:- true/false in lowercase like I am having for Contact_Relationship__c field and also the field names are not case sensitive but you need to take care of the underscores ( _ ) and the ending __c in case of custom fields.

So, using this method, you can get the whole request body, perform your operations and return an appropriate response from the API without having a lot of method parameters.

That's all for this tutorial, in the next tutorial we'll explore the DELETE method in detail. The whole code of this tutorial is available in the post branch of my SFDC-Integration-Tutorial github repository which can be viewed by clicking here.

Tired of reading or just scrolled down, don't worry, you can watch the video too.

If you liked this tutorial, make sure to share it in your network and let me know your feedback in the comments section below.

Happy Trailblazing..!!


  1. Really helpful, Thank you!
    i have a question, will this code work, if i have more than 1 contact?

    1. As of now, it won't work with more than one contact. You can update the parsing to a list of contacts as shown below:

      List<Contact> contacts = (List<Contact>) JSON.deserialize(requestBody, List<Contact>.class);

      now it should work with multiple contacts.