function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
eustachieustachi 

Creating/reading custom objects using REST API

Hi

 

I'm running the sample app from here http://wiki.developerforce.com/index.php/Getting_Started_with_the_Force.com_REST_API

 

The following code successfully creates a new Account:

 

 

PostMethod post = new PostMethod(instanceUrl
				+ "/services/data/v20.0/sobjects/Account/");

 

 

However, when I try to create the following custom object, I get a 400 error:

 

 

PostMethod post = new PostMethod(instanceUrl
				+ "/services/data/v20.0/sobjects/OpportunityAccount__c/");

 

I can see the custom object listed when I issue: 

GetMethod get = new GetMethod(instanceUrl
				+ "/services/data/v20.0/sobjects");

 

I get back the following: 

 

 {
      "activateable": false,
      "createable": true,
      "custom": true,
      "customSetting": false,
      "deletable": true,
      "deprecatedAndHidden": false,
      "feedEnabled": false,
      "keyPrefix": "a00",
      "label": "OpportunityAccount",
      "labelPlural": "OpportunityAccounts",
      "layoutable": true,
      "mergeable": false,
      "name": "OpportunityAccount__c",
      "queryable": true,
      "replicateable": true,
      "retrieveable": true,
      "searchable": true,
      "triggerable": true,
      "undeletable": true,
      "updateable": true,
      "urls": {
        "describe": "/services/data/v20.0/sobjects/OpportunityAccount__c/describe",
        "rowTemplate": "/services/data/v20.0/sobjects/OpportunityAccount__c/{ID}",
        "sobject": "/services/data/v20.0/sobjects/OpportunityAccount__c"
      }

 

 

Would appreciate any advice on what I may be doing wrong. 

 

Thanks

 

 

SuperfellSuperfell

Can you post the rest of your code, what's in the body of the response when you get the 400 response ?

eustachieustachi

 


SimonF wrote:

Can you post the rest of your code, what's in the body of the response when you get the 400 response ?


 

Thanks for the response.

 

By body of the response, do you mean the responseBody field of the PostMethod class?

 

If so, that comes back null.

 

I've pasted the full create method below. This is identical to the successful create for the non-custom object (which is from the code for the sample app ):

 

 

private String createOpportunityAccount(String name, String instanceUrl,
			String accessToken, PrintWriter writer) throws ServletException,
			IOException {
		String accountId = null;

		HttpClient httpclient = new HttpClient();

		JSONObject account = new JSONObject();

		try {
			account.put("Name", name);
		} catch (JSONException e) {
			e.printStackTrace();
			throw new ServletException(e);
		}

		PostMethod post = new PostMethod(instanceUrl
				+ "/services/data/v20.0/sobjects/OpportunityAccount__c/");

		post.setRequestHeader("Authorization", "OAuth " + accessToken);
		post.setRequestEntity(new StringRequestEntity(account.toString(),
				"application/json", null));

		try {
			httpclient.executeMethod(post);

			writer.write("HTTP status " + post.getStatusCode()
					+ " creating account\n\n");

			if (post.getStatusCode() == HttpStatus.SC_CREATED) {
				try {
					JSONObject response = new JSONObject(new JSONTokener(
							new InputStreamReader(
									post.getResponseBodyAsStream())));
					System.out.println("Create response: "
							+ response.toString(2));

					if (response.getBoolean("success")) {
						accountId = response.getString("id");
						writer.write("New record id " + accountId + "\n\n");
					}
				} catch (JSONException e) {
					e.printStackTrace();
					// throw new ServletException(e);
				}
			}
		} finally {
			post.releaseConnection();
		}

		return accountId;
	}

	

 

 

dkadordkador

Which version of HttpClient are you using?  It looks like you should be able tor call a method on the PostMethod object called getResponseBodyAsString().  Post that string here, please.  You shouldn't be getting an HTTP 400 with an empty response body.

 

Is your custom object part of an installed package (or are you in a namespaced DE org) by any chance?

Valli MuruganValli Murugan
What is the API to create the record in Custom Object of Salesforce?