Query Deleted Records in Salesforce: 3 Easy Methods

|

Query Deleted Records in Salesforce

Salesforce is a cloud-based customer relationship management software offered on a subscription payment model. Salesforce now includes not only customer relationship management but several other services that help an organization to carry out everything related to doing business. Offerings from Salesforce now include Salescloud, marketing cloud, business process automation services, etc.

Salesforce stores the organization data as objects. Objects come in three types – Standard objects, Custom objects, or external objects.  The business processes are implemented based on these objects. Salesforce provides options to edit, update or delete the object records to accomplish various business workflows. At times while working with Salesforce, there is a need to access the records that are deleted or archived. This post is about how to query deleted records in Salesforce.

Table of Contents

Need to Query deleted records in Salesforce

Query deleted records in Salesforce : Salesforce Logo
Image Source: upload.wikimedia.org

As discussed earlier, Salesforce business processes are accomplished by manipulating the object records.  One of the most common scenarios where there is a need to Query Deleted Records in Salesforce is when a business process failed because of non-existing records. . This happens when there are conflicting business processes developed by separate groups. When this happens, the only solution is to retrieve the deleted records and find what is the root cause. 

Another scenario is when records that are purposefully deleted are needed later for some kind of analysis. This happens commonly when there has been an initiative to purge duplicate records or cleanup up records based on time of origin. 

Some objects like activity-related ones are automatically archived when they are expired. Such objects then cannot be accessed via the user interface or the SOQL query interface. At times, it is required to access these objects for analysis. These are a few reasons for Query deleted records in Salesforce.

Simplify Data Analysis with Hevo’s No-code Data Pipeline

Hevo Data, a No-code Data Pipeline helps to load data from any data source such as Salesforce, Shopify, Google Search Console, Databases, SaaS applications, Cloud Storage, SDKs, and Streaming Services and simplifies the ETL process. It supports 100+ data sources (including 30+ free data sources) and is a 3-step process by just selecting the data source, providing valid credentials, and choosing the destination. Hevo not only loads the data onto the desired Data Warehouse/destination but also enriches the data and transforms it into an analysis-ready form without having to write a single line of code.

GET STARTED WITH HEVO FOR FREE[/hevoButton]

Its completely automated pipeline offers data to be delivered in real-time without any loss from source to destination. Its fault-tolerant and scalable architecture ensure that the data is handled in a secure, consistent manner with zero data loss and supports different forms of data. The solutions provided are consistent and work with different BI tools as well.

Check out why Hevo is the Best:

  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Schema Management: Hevo takes away the tedious task of schema management & automatically detects the schema of incoming data and maps it to the destination schema.
  • Minimal Learning: Hevo, with its simple and interactive UI, is extremely simple for new customers to work on and perform operations.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Incremental Data Load: Hevo allows the transfer of data that has been modified in real-time. This ensures efficient utilization of bandwidth on both ends.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
  • Live Monitoring: Hevo allows you to monitor the data flow and check where your data is at a particular point in time.
SIGN UP HERE FOR A 14-DAY FREE TRIAL

Methods to Query deleted records in Salesforce

Query Deleted Records in Salesforce: Records Images
Image Source: c1.sfdcstatic.com

Salesforce records can be programmatically accessed by using SOQL or SOSL queries. SOQL stands for Salesforce Object Query Language. SOSL stands for Salesforce Object Search language. Salesforce recommends using SOSL when you want to execute a search based on fields in objects and you do not know the specific objects you are looking for.

Both SOQL and SOSL are supported by the REST and SOAP APIs provided by Salesforce. Salesforce also provides APEX – A strongly typed object-oriented programming language that can be used to manipulate data while used in conjunction with the REST and SOAP APIs.

Whether you use the SOAP API, REST API, or the APEX interface, the key to Query Deleted Records in Salesforce is to frame the right query with the required parameters to access the deleted records. To access all the records irrespective of their deletion status, Salesforce requires one to use the QUERY ALL construct. Let us now look at the different mechanisms to Query deleted records in Salesforce.

  • Method 1: Accessing via REST API
  • Method 2: Accessing via SOAP API
  • Method 3: Accessing via APEX

Method 1: Accessing via REST API

The REST API provides a specific API called QueryALL to Query Deleted Records in Salesforce. Learn more about Rest API.

The input for the QueryAll API is a SOQL query that is meant to filter the specific records that you require.

The REST API request format is as below.

https://<your_salesforce_url>/services/data/vXX.X/queryAll/?q=SOQL query

If you are interested only in the deleted records and not any other records then you should add the isDeleted=True flag to your SOQL query.

Let’s see how this is done with an example. For example, if you want to access all the deleted records for a specific object called Merchandise__c, use the below query.

https://<your_salesforce_url>/services/data/v52.0/queryAll/?q=SELECT+Name+from+Merchandise__c+WHERE+isDeleted+=+TRUE

If the response contains more records than the threshold limit, Salesforce will provide a nextRecordsUrl. You can use this with the normal query API to fetch the remaining records. It is to be noted that for using the nextRecordsUrl, you do not need the queryAll API. 

Method 2: Accessing via SOAP API

The SOAP API also provides a query API to Query Deleted Records in Salesforce. Learn more about SOAP API

Since the SOAP API is generally used with a language binding like Java or C#, it provides a bit more streamlined way of programmatically accessing the deleted records. 

If you are using Java, you can use the connection.queryAll() statement with the required SOQL query. 

The code snippet to Query Deleted Records in Salesforce from the Account object will be as follows.

String soqlQuery = "SELECT Name, IsDeleted FROM Account";     
QueryResult qr = connection.queryAll(soqlQuery);

To retrieve items that are above the threshold limit, Java SOAP API provides a QueryMore construct. You can use this along with QueryLocator that you can retrieve from the QueryResult object.

Method 3: Accessing via APEX

Apex makes it easy to retrieve the deleted records by providing a SOQL query element called ALL ROWS. Please note that the ALL ROWS construct cannot be used with the REST or SOAP APIs and is exclusively meant for APEX-based access.

Query deleted records in Salesforce can be accessed via a SOQL query in APEX as below. 

Select Id, name from Account where ID = ‘<your ID here>’ and IsDeleted = true ALL ROWS

The above is used to query deleted records in Salesforce from the Account object. 

If you have access to Salesforce Workbench, you can Query Deleted Records in Salesforce by just enabling the ‘Include Deleted and Archived Records’. But sInce workbench is not an official salesforce product, it generally is not widely used in organizations. 

Conclusion

The above methods provide easy steps to access deleted records or all records from Salesforce. Salesforce Query Deleted Records is very critical to debugging failed process builder jobs in some cases. This feature is also useful when you want to analyze records that you have purposefully purged or cleanup up earlier.

If your organization is heavily dependent on Salesforce for customer relationship management, the odds are that you have a use case for combining data from other services with that coming from Salesforce to run a comprehensive analysis. Or you may need the data from salesforce to be pushed to a data warehouse to take advantage of analytics beyond what is provided by Salesforce. In any case, a cloud-based ETL tool like Hevo that can ingest data from almost any source can be a great asset.

While Query Deleted Records in Salesforce, it is a hectic task to Set Up the proper environment. To make things easier, Hevo comes into the picture. Hevo Data is a No-code Data Pipeline and has awesome 100+ pre-built Integrations that you can choose from.

visit our website to explore hevo[/hevoButton]

Hevo can help you Integrate your data from numerous sources like Asana and load them into a destination to Analyze real-time data with a BI tool such as Tableau. It will make your life easier and data migration hassle-free. It is user-friendly, reliable, and secure.

SIGN UP for a 14-day free trial and see the difference!

Share your experience of learning about Query Deleted Records in Salesforce in the comments section below.

Talha
Software Developer, Hevo Data

Talha is a seasoned Software Developer, currently driving advancements in data integration at Hevo Data, where he have been instrumental in shaping a cutting-edge data integration platform for the past four years. With a significant tenure at Flipkart prior to their current role, he brought innovative solutions to the space of data connectivity and software development.

No-code Data Pipeline For Salesforce