This is possible in java. I tried your code and it is working. As per my observation only thing which could be culprit here is the uri.
Below is the code snippet where I have mentioned uri directly.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
JSONParser parser = new JSONParser();
// Parser parsing JSON File having multiple records
Object obj = parser.parse(new FileReader("JsonData.json"));
HttpClient httpclient = new HttpClient();
PostMethod mypost = new PostMethod("https://kmforce-dev-ed.my.salesforce.com/services/data/v34.0/composite/tree/Account/");
String jsonText = obj.toString();
System.out.println("\n_JSON TEXT=" + jsonText);
mypost.setRequestHeader("Authorization", "OAuth " +"<myssessionid>");
mypost.setRequestEntity(new StringRequestEntity(jsonText, "application/json", "UTF-8"));
int response=httpclient.executeMethod(mypost);
System.out.println(response);
System.out.println("Response-->"+mypost.getResponseBodyAsString());
}
}
Below is the output:
{
"hasErrors": false,
"results": [
{
"referenceId": "ref1",
"id": "0019000001bslYJAAY"
},
{
"referenceId": "ref2",
"id": "0019000001bslYKAAY"
},
{
"referenceId": "ref3",
"id": "0019000001bslYLAAY"
},
{
"referenceId": "ref4",
"id": "0019000001bslYMAAY"
}
]
}
Let me know if you face any issue here.
You can definitely use workbench to execute these
Use the below for the request
/services/data/v34.0/composite/tree/Account
and in the body use the below JSON
{
"records" :[{
"attributes" : {"type" : "Account", "referenceId" : "ref1"},
"name" : "SampleAccount1",
"phone" : "1111111111",
"website" : "www.salesforce1.com",
"numberOfEmployees" : "100",
"industry" : "Banking"
},{
"attributes" : {"type" : "Account", "referenceId" : "ref2"},
"name" : "SampleAccount2",
"phone" : "2222222222",
"website" : "www.salesforce2.com",
"numberOfEmployees" : "250",
"industry" : "Banking"
},{
"attributes" : {"type" : "Account", "referenceId" : "ref3"},
"name" : "SampleAccount3",
"phone" : "3333333333",
"website" : "www.salesforce3.com",
"numberOfEmployees" : "52000",
"industry" : "Banking"
},{
"attributes" : {"type" : "Account", "referenceId" : "ref4"},
"name" : "SampleAccount4",
"phone" : "4444444444",
"website" : "www.salesforce4.com",
"numberOfEmployees" : "2500",
"industry" : "Banking"
}]
}
You should see the response in workbench
Alternatively you can use POSTMAN to experiment with this API .
You will need to create a connected app in salesforce and use oauth 2.0 to get access token for subsequent calls
use the below link for reference
Best Answer
ContentDocument is not directly created - if you submit a ContentVersion without setting ContentDocumentId, it will create a new ContentDocument. You won't get the ContentDocument's Id in the response, so you'd need to retrieve or query the new ContentVersion to get the ContentDocument's Id for creating ContentDocumentLink records.
But, when creating a new ContentDocument in this way, you can also create an initial ContentDocumentLink by way of the FirstPublishLocationId field. From the Object Reference: