Inserting data into Salesforce using JAVA (Web Service API)

To insert data from external application to Salesforce, create() method is used.

create ()

create () add one or more new records to your organization’s data. Use create () to add one or more records, such as an Account or Contact record, to your organization’s information. create () call is analogous to the INSERT statement in SQL.

Syntax

SaveResult[] = connection.create(sObject[] sObjects);

where connection is an object of EnterpriseConnection.

Sample JAVA Program to Insert data into Salesforce

package wsc;

public class InsertAccount
{
            public String authEndPoint = "";
            EnterpriseConnection con;
            private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            public static void main(String[] args)
            {          
                        InsertAccount sample = new InsertAccount("https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r");
                        if ( sample.login() )
                        {
                                    sample.insertAcct();
                        }
            }
            public InsertAccount(String authEndPoint)
            {
                        this.authEndPoint = authEndPoint;
            }
            public String getUserInput(String prompt)
            {
                        String result = "";
                        try
                        {
                                    System.out.print(prompt);
                                    result = reader.readLine();
                        }
                        catch (IOException ioe)
                        {
                                    ioe.printStackTrace();
                        }
                        return result;
            }
            public boolean login()
            {


                        boolean success = false;
                        String userId = getUserInput("UserID: ");
                        String passwd = getUserInput("Password + Security Token: ");
                        try
                        {
                                    /* Setting up Username and Password */
                                    ConnectorConfig config = new ConnectorConfig();
                                    config.setAuthEndpoint(authEndPoint);
                                    config.setUsername(userId);
                                    config.setPassword(passwd);
                                    config.setCompression(true);
                                    //config.setProxy("Your Proxy", 80);
                                    System.out.println("AuthEndPoint: " + authEndPoint);
                                    config.setTraceFile("insertLogs.txt");
                                    config.setTraceMessage(true);
                                    config.setPrettyPrintXml(true);

                                    System.out.println("\nLogging in ...\n");
                                    /* Setting up connection to Salesforce */
                                    con = new EnterpriseConnection(config);
                                    GetUserInfoResult userInfo = con.getUserInfo();   
                                    System.out.println("UserID: " + userInfo.getUserId());
                                    System.out.println("\nLogged in Successfully\n");
                                    success = true;
                        }
                        catch (ConnectionException ce)
                        {
                                    ce.printStackTrace();
                        }
                        catch (FileNotFoundException fnfe)
                        {
                                    fnfe.printStackTrace();
                        }
                        return success;
            }
            public void insertAcct()
            {
                        /* Getting Account details */
                        String acctName = getUserInput("Enter Account name:");
                        String acctNo = getUserInput("Enter Account no:");
                        String phoneNo = getUserInput("Enter phone no:");
                        String extId = getUserInput("Enter External Id:");
                        Account acct = new Account();
                        /* Setting Account details */
                        acct.setName(acctName);
                        acct.setPhone(phoneNo);
                        acct.setAccountNumber(acctNo);
                        acct.setExternal_ID__c(extId);
                        Account[] accts = { acct };

                        try
                        {                                  
                                    System.out.println("\nInserting...\n");
                                    /* Inserting account records */
                                    con.create(accts);
                        }
                        catch (ConnectionException ce)
                        {
                                    ce.printStackTrace();
                        }
                        System.out.println("\nAccount has been inserted successfully.");
            }
}

Output:

UserID: chennai@infallible.com
Password + Security Token: chennaiuytrfghikolmnhB1kFNW9kEw
AuthEndPoint: https://login.salesforce.com/services/Soap/c/24.0/0DF90123456lko8r
[WSC][InsertAccount.login:61]Log file already exists, appending to insertLogs.txt
Logging in ...
UserID: 00590000000qXHsAAM
Logged in Successfully
Enter Account name:Test SFDC POC
Enter Account no:82827
Enter phone no:8877665544332
Enter External Id:90
Inserting...
Account has been inserted successfully.

11 comments:

  1. how to use WSC package ,please mention how use it ,

    ReplyDelete
    Replies
    1. Check this for WSC usage

      http://www.infallibletechie.com/2012/11/java-to-salesforce-connection.html

      Cheers!!!

      Delete
  2. What is "0DF90000000PX8r" that you have appended at the end of ypu authEndPoint?
    Do we have to use diffirent authEndPoint in case I am creating a new contact in salesforce

    ReplyDelete
    Replies
    1. It is provided by Salesforce for SOAP API request.

      Delete
  3. How do I generate it??...Can u share some resources for help?

    ReplyDelete
    Replies
    1. Hi Can you please provide more detailed information on "0DF90000000PX8r"

      Delete
  4. @Magulan Duraipandian grt work..I just want to know y u used "config.setTraceMessage(true); config.setPrettyPrintXml(true);".Thank u in advance.

    ReplyDelete
    Replies
    1. setTraceMessage() is used to track the logs. The log will be in XML format. setPrettyPrintXml() will beautify the XML.

      Delete
  5. Hi,
    In one of my requirement i have to upload 20000 records in to salesforce custom object using Java and one of the field has a Look up to Account. So before inserting the records we need to write a trigger which updates all Account IDs before inserting them. In this case how many records we can upload in a single insertion..?
    Please let me know the limitations.
    Note: We have not used the Bulk API process.

    -Thanks,
    CK

    ReplyDelete
    Replies
    1. Hi,
      I have requirement.. whenever a record is created in Java based web application, the same record has to be created on Product object in salesforce. Could anyone please let me know how we can achieve this?

      Thanks

      Delete
  6. Hi,
    I have requirement.. whenever a record is created in Java based web application, the same record has to be created on Product object in salesforce. Could anyone please let me know how we can achieve this?

    Thanks

    ReplyDelete