1

I am writing a JAVA-application that mainly functions as a CRUD-platform for my employees. On this application, users can manage data from a specific objects. I am using REST-API.

Now, here is my method for inserting values:

  public void createTimeTracking(String subject, String date) {
    System.out.println("\n_______________ Time-Tracking INSERT _______________");

    String uri = baseUri + "/sobjects/Time_Tracking__c/";
    try {

        //create the JSON object containing the new lead details.
        JSONObject tt = new JSONObject();

        tt.put("TimeTrackingRecordTypeId", chosenProject.getProjectRecordTypeId());
        tt.put("Subject__c", subject);
        tt.put("Project__c", getChosenProject().getProjectId());
        tt.put("Work_Package__c", getChosenWorkPackage().getWorkPackageId());
        tt.put("Date__c", date);
        tt.put("Internal_Contact__c", benutzer.getInternalContactId());
        tt.put("Status__c", "Submitted by Employee");

        System.out.println("JSON for car record to be inserted:\n" + tt.toString(1));

        //Construct the objects needed for the request
        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost(uri);
        httpPost.addHeader(oauthHeader);
        httpPost.addHeader(prettyPrintHeader);
        // The message we are going to post
        StringEntity body = new StringEntity(tt.toString(1));
        body.setContentType("application/json");
        httpPost.setEntity(body);


        //Make the request
        HttpResponse response = httpClient.execute(httpPost);

        //Process the results
        int statusCode = response.getStatusLine().getStatusCode();

        String response_string = EntityUtils.toString(response.getEntity());
        JSONObject json = new JSONObject(response_string);

        if (statusCode == 201) {
            // Store the retrieved lead id to use when we update the lead.
            String ttId = json.getString("id");
            System.out.println("New TT id from response: " + ttId);
        } else {

            System.out.println("Insertion unsuccessful. Status code returned is " + statusCode);
        }
    } catch (JSONException e) {
        System.out.println("Issue creating JSON or processing results");
        System.out.println(e.toString());
    } catch (IOException | NullPointerException ioe) {
        System.out.println(ioe.getMessage());
    }
}

I get the following error:

Issue creating JSON or processing results
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

Now, I can exclude that any other record but "RecordTypeId" is causing this issue, because I can insert the rest of the values without any JSON-Exception.

The RecordTypeId is valid, but apparently it's not, when it keeps failing to proceed the request..

I would appreciate any help. Thank you.

  • RecordTypeId field will be available if recordType is enabled for that sobject. – Ratan Paul Jan 28 '16 at 8:13
0

The RecordType field should be called RecordTypeId not TimeTrackingRecordTypeId. This usage of an invalid field name is causing the API to return an error response, error response are arrays in json, not a json object, so your attempt to always decode the response as a json object fails in this case. You need to check the status code to see if its 2xx before trying to decode the response as a json object, or if the status code is >= 300 decode the response as a json array instead.

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.