Emptying recycle bin in Salesforce using JAVA (Web Service API)

Emptying recycle bin in Salesforce using JAVA (Web Service API)

To empty recycle bin data in Salesforce using external
application, emptyRecycleBin () method is
used.

emptyRecycleBin ()
Delete
records from the recycle bin immediately. The recycle bin lets you view and
restore recently deleted records for 15 days before they are permanently
deleted. Your Organization can have up to 5,000 records per license in the
Recycle Bin at any one time. For example, if your organization has five user
licenses, 25,000 records can be stored in the Recycle Bin. If your organization
reaches its Recycle Bin limit, Salesforce automatically removes the oldest
records, as long as they have been in the recycle bin for at least two hours. If
you know you will be adding a great number of records to the Recycle Bin and
you know you won’t need to undelete () them, you may wish to remove them before
the Salesforce process deletes records. For example, you can use this call if
you are loading a large number of records for testing, or if you are doing a
large number of create ()calls followed by delete() calls.
Syntax
EmptyRecycleBinResult[]
= connection.emptyRecycleBin(ID[] ids);
where connection is an object of EnterpriseConnection.
Sample JAVA Program to
empty recycle bin 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.EmptyRecycleBinResult;
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.soap.enterprise.Error;
import
com.sforce.ws.ConnectionException;
import
com.sforce.ws.ConnectorConfig;

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

            public static void main(String[] args)
            {          
                        EmptyRecyleBin
sample =
new
EmptyRecyleBin(
“https://login.salesforce.com/services/Soap/c/24.0/0DF90000000PX8r”);
                        if ( sample.login() )
                        {
                                    sample.emptyRecycle ();
                        }
            }
            public EmptyRecyleBin(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(“nAuthEndPoint: “ + authEndPoint + “n”);
                                    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 emptyRecycle ()
            {
                        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);
                                    EmptyRecycleBinResult[]
emptyRecycleBinResults =
                                                con.emptyRecycleBin(ids);
                                    /* Checking whether any error has been reported
                                     * while emptying the recycle bin*/
                                    for (int i = 0; i <
emptyRecycleBinResults.
length; i++)
                                    {
                                                EmptyRecycleBinResult
emptyRecycleBinResult =
                                                            emptyRecycleBinResults[i];
                                                if (emptyRecycleBinResult.isSuccess())
                                                {
                                                            System.out.println(“Recycled ID: “ +
                                                                                    emptyRecycleBinResult.getId());
                                                }
                                                else
                                                {
                                                            Error[]
errors = emptyRecycleBinResult.getErrors();
                                                            if (errors.length > 0)
                                                            {
                                                                        System.out.println(“Error code: “ +
                                                                                                errors[0].getStatusCode());
                                                                        System.out.println(“Error message: “ +
                                                                                                errors[0].getMessage());
                                                            }
                                                }
                                    }
                                    System.out.println(“nAccount has been deleted successfully.nn”);
                                    System.out.println(“nAfter Deletingnn”);
                    sql = “SELECT
Name,Id FROM Account WHERE Name Like ‘%”
+ acctName + “%'”;                  
                    result = con.query(sql);
                                    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”);
                                    }
                        }
                        catch (ConnectionException
ce)
                        {
                                    ce.printStackTrace();
                        }
            }
}
Output:

Password + Security Token: fgfuserfgfgnhHUwB1kFNW9kEw
AuthEndPoint:
https://login.salesforce.com/services/Soap/c/24.0/0DF900fgsfgPX8r
[WSC][EmptyRecyleBin.login:65]Log
file already exists, appending to deleteLogs.txt
Logging in …
UserID: 00590sfgf0qXHsAAM
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    0019000000BOOFMAA5    null
Enter the Account Id to
delete:0019000000BOLBJAA5
Deleting…
Recycled ID: 0019000000BOLBJAA5
Account has been deleted
successfully.
After Deleting
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    0019000000BOOFMAA5    null

Leave a Reply