How to Create record over http POST request without hardcoded JSON Mapping.


In this blog, we will learn how we can implement web service and that too even without direct mapping any field name from JSON.


Let's start,

1) Create a new class and name it according to you.

2) Annotate it as shown below:

3) Create a method and annotate it so that while calling it from an external service, it will get to know that this is the post method from where I have to start executing.

4) Now we are getting the data that was sent with the post method as the body. Here we are deserializing the received JSON and then typecasting it into the map.

5) Now we need to declare some global scope lists so that we can store details about fields in that. Below variables should be declared global i.e. outside method but inside the class. After that, we initialize those before calling out the function in which we are going to get the metadata.

6) Now to get the object and objects field names from salesforce by using Schema (Metadata), create another method in which we will get the field names, field labels, and field data types.

7) Now let's loop over the map which we got after deserializing the JSON data. For that need to create another method as given below.

8) Now inside the switch, we need to check for every datatype and assign the appropriate value to the appropriate field.

I have listed some of them below :

From above the tricky one is the only address we need to typecast map entry once again to map.


Now, let's test the web service whether it is working or not.


1) log in to the workbench.

2) To call the web service from workbench we need "/services/apexrest/PostMethodExample" and pass the data in the body by selecting the HTTP method as POST.

In the above JSON, we have used the API names from salesforce as a key so that there is no need for any code if any new field needs to be added to the web service. Just add it here and the remaining work will be done automatically.


The final code should look like this.


To deploy this as a public web service visit - This


Thank you for visiting cloudwaale, please share your view in comment.

68 views0 comments

Recent Posts

See All