Updating data into Salesforce using JAVA (Web Service API)

Updating data into Salesforce using JAVA (Web Service API)

To update data in Salesforce using external application, update () method is used.

update ()

Update
one or more existing records in your organization’s data. Use this call
to update one or more existing records, such as accounts or contacts,
in your organization’s data. The update () call is analogous to the
UPDATE statement in SQL.

Syntax

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

where connection is an object of EnterpriseConnection.

Sample JAVA Program to update data in Salesforce

package wsc;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.GetUserInfoResult;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

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

            public static void main(String[] args)
            {          
                        UpdateAccount sample = new UpdateAccount(“https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r”);
                        if ( sample.login() )
                        {
                                    sample.updateAcct();
                        }
            }
            public UpdateAccount(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(“updateLogs.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 Successfullyn”);
                                    success = true;
                        }
                        catch (ConnectionException ce)
                        {
                                    ce.printStackTrace();
                        }
                        catch (FileNotFoundException fnfe)
                        {
                                    fnfe.printStackTrace();
                        }
                        return success;
            }
            public void updateAcct()
            {
                        try
                        {                      
                                    /* Getting Account details */
                                    Account[] updates = new Account[1];
                                    Account account01 = new Account();
                                    String id = “”;
                                    String acctNo = “”;        
                                    String acctName = getUserInput(“Enter Account name to search:”);
                    String sql = “SELECT Name,Id FROM Account WHERE Name Like ‘%” + acctName + “%'”;         
                    QueryResult result = con.query(sql);
                                    SObject[] records = result.getRecords();
                                    System.out.println(“nAccount Name                   Id                  Billing City n”);
                                    for ( int i = 0; i < records.length; ++i )
                                    {
                                                Account accnt = (Account) records[i];
                                                String sfdcId = accnt.getId();
                                                String actName = accnt.getName();
                                                String billingCity = accnt.getBillingCity();
                                                System.out.println(“n” + actName +     + sfdcId +     + billingCity + “n”);
                                    }
                                    id = getUserInput(“Enter the Account Id to update:”);
                                    account01.setId(id);
                                    acctNo = getUserInput(“nEnter the new Account no to update:”);
                                    account01.setAccountNumber(acctNo);
                                    updates[0] = account01;
                                    System.out.println(“nUpdating…n”);
                                    /* Updating account records */    
                                    con.update(updates);
                        }
                        catch (ConnectionException ce)
                        {
                                    ce.printStackTrace();
                        }
                        System.out.println(“nAccount has been updated successfully.”);
            }
}
Output:

Password + Security Token: tyser01xmJO3dfdqUndfdfkdfkEw
AuthEndPoint: https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r
[WSC][UpdateAccount.login:63]Log file already exists, appending to updateLogs.txt
Logging in …
UserID: 005900dfde0qXHsAAM
Logged in Successfully
Enter Account name to search:test
Account Name                   Id                  Billing City
Test_Acct3    0019000000Ad2tJAAR    null
Test Acct3    0019000000AcfBFAAZ    null
Test Acct    0019000000AceoOAAR    null
Test Acct1    0019000000AcevzAAB    null
Test Acct2    0019000000Acew0AAB    null
test poc    0019000000BOP0SAAX    null
Test SFDC POC    0019000000BOPWmAAP    null
test_accccc    0019000000BOLBJAA5    null
Test Java1    0019000000BOOFMAA5    null
Enter the Account Id to update:0019000000BOP0SAAX
Enter the new Account no to update:0098
Updating…
Account has been updated successfully.

Leave a Reply