Deleting data from Salesforce using JAVA (Web Service API)

Deleting data from Salesforce using JAVA (Web Service API)

To delete data from Salesforce using external application, delete
() method is used.

delete()

Delete
one or more records from your organization’s data. Use delete () to delete one
or more existing records, such as individual accounts or contacts, in your
organization’s data. The delete () call is analogous to the DELETE statement in
SQL.


Syntax
DeleteResult[]
= connection.delete(ID[] ids);
where connection is an object of EnterpriseConnection.

Sample JAVA Program to
delete data from 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 DeleteAccount
{

            public String authEndPoint = “”;

            EnterpriseConnection
con;

            private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


            public static void main(String[] args)

            {          

                        DeleteAccount
sample =
new DeleteAccount(“https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r”);

                        if ( sample.login() )

                        {

                                    sample.deleteAcct();

                        }

            }

            public DeleteAccount(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(“deleteLogs.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 deleteAcct()

            {

                        try

                        {                      

                                    /* Getting Account details */

                                    String
acctName = getUserInput(
“Enter Account
name to search:”
);

                                    String
id =
“”;                            

                    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
delete:”
);

                                    System.out.println(“nDeleting…n”);

                                    /* Deleting account records */     

                                    String[]
ids = {id};

                                    con.delete(ids);

                        }

                        catch (ConnectionException
ce)

                        {

                                    ce.printStackTrace();

                        }

                        System.out.println(“nAccount has been deleted successfully.”);

            }

}

Output:

Password + Security Token: chennaiuytrfghikolmnhB1kFNW9kEw
AuthEndPoint:
https://login.salesforce.com/services/Soap/c/24.0/0DF90123456lko8r
[WSC][DeleteAccount.login:63]Log
file already exists, appending to deleteLogs.txt
Logging in …
UserID: 00590000000qXHsAAM
Logged in Successfully
Enter Account name to
search:test
Account Name                   Id                  Billing City
Test_Acct3    0019000000Ad2tJAAR    null
test.doc    0019000000AcvdnAAB    null
Test Acct3    0019000000AcfBFAAZ    null
Test Acct    0019000000AceoOAAR    null
Test Acct1    0019000000AcevzAAB    null
Test Acct2    0019000000Acew0AAB    null
test poc    0019000000BOP0SAAX    null
Arun_test_Acc    0019000000Acs31AAB    null
Test SFDC POC    0019000000BOPWmAAP    null
test.doc    0019000000AcvbHAAR    null
test_accccc    0019000000BOLBJAA5    null
Test Java1    0019000000BOOFMAA5    null
Enter the Account Id to
delete:0019000000AcvbHAAR
Deleting…

Account has been deleted
successfully.

Leave a Reply