Creating objects in Salesforce using REST API from Java

Posted on 09-01-2013 13:45 by graham
This tutorial shows you how to insert a new record into Salesforce using the RESTful API.

The REST API service


Salesforce exposes a REST API that allows you to manipulate data. The API is available at
https://<instance-name>/services/data/v20.0/sobjects/<your-sobject-api-name>

Assuming you have an object called MyObject__c, and you are operating on a Salesforce instance called na4.salesforce.com, your REST service to manipulate objects of this type will be:
https://na4.salesforce.com/services/data/v20.0/sobjects/MyObject__c

Serializing data to JSON


Let's say you have created an instance of MyObject__c. To pass it to the REST call, you need to serialize it to the JSON format. The result of the serialization should look something like this:
{
"Name": "Some object name",
"Property_one__c": "value 1",
"Property_two__c": "value 2"
}

How you perform the serialization is up to you. There are plenty of Java libraries that can do this for you.

Sending the request


The HTTP request that creates an object needs to use the POST method. In order to be able to perform this operation, you also need to be authenticated and an access token that you will use in the OAuth authentication. Take a look at our tutorial on authenticating to Salesforce using OAuth.
String serializedObject = "{ \"Name\": \"Some object name\", \"Property_one__c\": \"value 1\", \"Property_two__c\": \"value 2\"	}";

HttpClient client = new HttpClient();

PostMethod method = new PostMethod("https://na4.salesforce.com/services/data/v20.0/sobjects/MyObject__c");

// add the object's data to the request body
method.setRequestEntity(new StringRequestEntity(serializedObject, "text/plain", "UTF-8"));
method.setRequestHeader("Content-Type", "application/json");

String sessionId = "jdskJK78jkJAUIS870QQndjkl89WkMMSPoop8S8982";
method.addRequestHeader("Authorization", "OAuth " + sessionId);

// this line is optional, but it will make your response look formatted
method.addRequestHeader("X-PrettyPrint", "1");

int statusCode;
try
{
// send the request
statusCode = client.executeMethod(method);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}

// check if execution succeeded - the create method returns code 201
if (statusCode != 201)
{
// success
System.out.print
}

If everything goes fine, you should receive a response that looks something like this:
{
"id":"0017000000dY6KIAB0",
"errors":[],
"success":true
}

Inserting collections of objects


Seeing the above solution you might wonder whether it would be possible to insert whole collections of objects into Salesforce in one call. Unfortunately, this feature is not supported. If you try to pass a JSON array instead of a single object into the insert request, you will be a BAD REQUEST response.

Of course, there are workarounds for this. One of them is to create a custom REST service on your organization and insert objects using this service.
Comments
Hi ,

Nice post. Actually i am looking for some thing very exact to what you mentioned about passing collections of object in salesforce for creation using custom REST api , but i am not able to find how to do it as i am new to coding , can you help with post showing how to create custom REST resource and pass json array or guide me to a link where its mentioned.

Thanks in Advance
Talwinder Singh
Added on 21-04-2013 12:37 by anonymous

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems