0

I am trying to create an object in a custom table through Salesforce REST API. The table is called B25__Reservation__c. The application is Booker25. Here is the code:

    CloseableHttpClient httpClient = HttpClients.createDefault();

    List<NameValuePair> loginParams = new ArrayList<NameValuePair>();
    loginParams.add(new BasicNameValuePair("client_id", CONSUMER_KEY));
    loginParams.add(new BasicNameValuePair("client_secret", CONSUMER_SECRET));
    loginParams.add(new BasicNameValuePair("grant_type", "client_credentials")); 
    
    HttpPost post = new HttpPost(LOGINURL_FULL);
    post.setEntity(new UrlEncodedFormEntity(loginParams));

    final HttpResponse loginResponse = httpClient.execute(post);

    final ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
    JsonNode loginResult = mapper.readValue(loginResponse.getEntity().getContent(), JsonNode.class);
    
    String accessToken = loginResult.get("access_token").asText();
    String instanceUrl = loginResult.get("instance_url").asText();
    
    JSONObject data = new JSONObject();
    data.put("B25__Title__c", "Test title");
    data.put("B25__Start__c", "2023-02-25T13:00:00.000+0000");
    data.put("B25__End__c", "2023-02-25T18:00:00.000+0000");
    data.put("B25__Start_Date__c", "2023-02-25");
    data.put("B25__End_Date__c", "2023-02-25");
    data.put("B25__Resource__c", "a3C8c00230ddd000");
    data.put("Reserved_For__c", "003DV0000234545");
    data.put("OwnerId", "0058c00000B123455");
            
    String uri = instanceUrl + "/services/data/v56.0/sobjects/B25__Reservation__c";
    HttpPost httpPost = new HttpPost(uri);
    
    httpPost.addHeader("Authorization", "Bearer " + accessToken);
    httpPost.addHeader(new BasicHeader("X-PrettyPrint", "1"));
            
    StringEntity body = new StringEntity(data.toString(1));
    body.setContentType("application/json");
    httpPost.setEntity(body);
    
    HttpResponse queryResponse = httpClient.execute(httpPost);

When running the code, I always get "Bad Request" response, and there is no additional information. For the same data and end point, I tried it at https://workbench.developerforce.com/, there was no problem creating the object.

I did a test by changing the end point to

"/services/data/v56.0/sobjects/Account"

and data to

    JSONObject data = new JSONObject();
    data.put("Name", "Test name");

the above code works without any problem (The response came back with the 201 status)

Anything wrong with the above code for creating an object in a custom table?

Update

Does anybody know what is the possible cause for this type of error? I was able to create a custom object in another table (conference360__Event__c) by changing the end point and the data in the above code.

I also tried to find out the logs in Salesforce and was not able to find out such info. I am not sure whether I can configure Salesforce to record errors related to THIS code.

I am new to Salesforce, and any help would be really appreciated.

curious1
  • 14,155
  • 37
  • 130
  • 231

0 Answers0