3

I Was of view, through rest API , only single record can be created in SFDC but this link has left me intrigued

Create Multiple records Via Rest

Through CURL using above tree resource, I'm able to create multiple records via Rest, at once . But I've question, how can I use this tree resource in JAVA Program to create records? Is this possible ?

I was trying something like this :

  JSONParser parser = new JSONParser();

  // Parser parsing  JSON File having multiple records
 Object obj = parser.parse(new FileReader("C:/Users/ABCuser/Documents/CURL/NewAccount.JSON"));

   httpclient = new HttpClient();
   PostMethod mypost = new PostMethod(uri);
   String jsonText = obj.toString();
   System.out.println("\n_JSON TEXT=" + jsonText);
   mypost.setRequestHeader("Authorization", "OAuth " + accessToken); 
   mypost.setRequestEntity(new StringRequestEntity(jsonText, "application/json", "UTF-8"));
   int response=httpclient.executeMethod(mypost);

At last , it just throws error with error code 404:

[{"errorCode":"NOT_FOUND","message":"The requested resource does not exist"}]

my question : My reason is, if this works in CURL, then it should be working in JAVA also, but I don't know, how I should proceed.

  • 1
    Let us know the endpoint you are using in Java Program. Please make sure that it has proper instance name. – Kiran Machhewar Dec 13 '15 at 17:52
1

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.

  • Wow thanks for pointing out mistake .. error was indeed in uri..... I was pointing to v32.0 in uri which seemed to be causing problem – curious_techy Dec 14 '15 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.