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.
Hevo simplifies data integration from Salesforce to your data warehouse, no coding required!
Why Hevo for Salesforce?
- Automated Data Pipelines: Connect Salesforce seamlessly to your analysis platform for holistic insights.
- Real-time Data Sync: Keep Salesforce data up-to-date in real-time for timely, informed decision-making.
- Transformation Capabilities: Enrich, clean, and transform Salesforce data for deeper insights.
Unlock the power of Salesforce data with Hevo’s secure and scalable platform.
Get Started with Hevo for Free
Need to Query deleted records in Salesforce
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.
Methods to Query deleted records in Salesforce
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.
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.
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.
If you need to query deleted records, our comprehensive guide on delete SOQL query can help. It includes examples and tips for efficiently finding and accessing the records you need.
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.
Learn how to create a duplicate report in Salesforce with our step-by-step guide to managing and eliminating duplicate records.
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.
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.
Try Hevo’s 14-day free trial and see the difference!
FAQs
1. Can you query deleted records in Salesforce?
Yes, you can query deleted records by using the ALL ROWS keyword in a SOQL query.
2. How to query deleted reports in Salesforce?
Deleted reports are not directly accessible through SOQL, but you can check the Recycle Bin in Salesforce to find and restore them.
3. How do I find deleted items in Salesforce?
Go to the Recycle Bin to view and restore recently deleted items.
Talha is a Software Developer with over eight years of experience in the field. He is currently driving advancements in data integration at Hevo Data, where he has been instrumental in shaping a cutting-edge data integration platform for the past four years. Prior to this, he spent 4 years at Flipkart, where he played a key role in projects related to their data integration capabilities. Talha loves to explain complex information related to data engineering to his peers through writing. He has written many blogs related to data integration, data management aspects, and key challenges data practitioners face.