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
Suganya SubrayanSuganya Subrayan 

How to insert the data to salesforce?

Hi,

How to insert the data to salesforce using java?

Thanks in advance
Tej PalTej Pal
HI Suganya,

Use JAVA sdk for it. Reference URL : https://developer.salesforce.com/page/Java

If this answers your question mark Best Answer it as solution and then hit Like!
Suganya SubrayanSuganya Subrayan
Hi Tej Pal,

Thanks for your quick response.
I have tried to create the record to salesforce. But, account created in that.

This is my java code for create the record.

public String createRecord(String name, String instanceUrl, String accessToken, PrintWriter writer) throws ServletException, IOException {
    String accountId = null;
    CloseableHttpClient httpclient = HttpClients.createDefault();
    JSONObject account = new JSONObject();
    try {
       account.put("Name", "Suganya");
       account.put("Gender__c", "Female");
    } catch (JSONException e) {
       e.printStackTrace();
       throw new ServletException(e);
    }
    HttpPost httpost = new HttpPost(instanceUrl + "/services/data/v30.0/sobjects/Account/");
    httpost.addHeader("Authorization", "OAuth " + accessToken);
    StringEntity messageEntity = new StringEntity(account.toString(), ContentType.create("application/json"));
    httpost.setEntity(messageEntity);
    // Execute the request.
    CloseableHttpResponse closeableresponse = httpclient.execute(httpost);
    System.out.println("Response Status line :" + closeableresponse.getStatusLine());
    try {
       writer.write("HTTP status " + closeableresponse.getStatusLine().getStatusCode() + " creating account\n\n");
       if (closeableresponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
        try {
           // Do the needful with entity.
           HttpEntity entity = closeableresponse.getEntity();
           InputStream rstream = entity.getContent();
           JSONObject authResponse = new JSONObject(new JSONTokener(rstream));
           System.out.println("Create response: " + authResponse.toString(2));
           if (authResponse.getBoolean("success")) {
            accountId = authResponse.getString("id");
            writer.write("New record id " + accountId + "\n\n");
           }
        } catch (JSONException e) {
           e.printStackTrace();
           // throw new ServletException(e);
        }
       }
    } finally {
       httpclient.close();
    }
    return accountId;
   }


Is this is correct or not??

Thanks in advance.