You are here:
Unarchive Apex
Programmatically unarchive records in the Archive app based on specific filters and retrieve search results that match your request.
Unarchiving a record also unarchives its entire hierarchical tree of root and child records.
Unarchive requests via Apex are limited to 50 per hour per org.
If you exceed this limit, you receive HTTP status code 429.
Prerequisites
Enable Archive Enable Unarchive to unarchive records from the Archive app or via Apex. If you need only one unarchive permission, enable one of these custom permissions and manually assign it to a custom permission set or profile.
- Archive Unarchive: Permission to unarchive records from the Archive app only.
- Archive Unarchive SDK: Permission to unarchive records via Archive SDK only.
Unarchive Apex functions
Search one object per unarchive request and apply a maximum of six filters to the search.
performUnarchiveSdk(String sObjectName, List<SearchFilter> filters, DateRange dateRange)
performUnarchiveSdk(String sObjectName, List<SearchFilter> filters)Apex parameters
- sObjectName
- The object queried by Apex. This parameter is required.
- Search Filters
- Filters that narrow search results based on field conditions. This parameter is required.
- Field Name: The field to filter by, such as
NameorCreatedDate. - Value or List of Values: The specific value or values to match. If you use a list of values, Apex Data Queries combines them with an
ORoperator.
- Field Name: The field to filter by, such as
- Date Filters
- To search based on dates, use the
DateRangeparameter and the required filters.DateRangefilters records by fields such asArchiveDate,CreatedDate, andModifiedDate. This parameter is optional. Date filter recommendations:- Add the
archive_datedate field to search based on the archive date. - Use the Apex date format: MM/DD/YYYY.
- Add the
Returned values
The return value is ArchiverAccessorResponse.
- Request ID of the unarchive operation performed
- Total number of records retrieved by the search
- Status code, including errors
ArchiverAccessorResponse.getBody(); ⇒ String
ArchiverAccessorResponse.getStatusCode(); ⇒ integer
200 - if the request was accepted
400 - if the request wasn't accepted
ArchiverAccessorResponse.getErrorMessage(); ⇒ StringUnarchive Apex scenarios
Review examples of the unarchive Apex process in the Archive app.
Scenario 1: Fewer than 1,000 matching records found, request processed—Success!
You want to unarchive all candidates who have a bachelor's degree.
- You execute the Archive Apex Unarchive function with the filter
has BA=true. - The system searches the candidate list and finds fewer than 1,000 archived records.
- A new activity of type Unarchive SDK is added, and all unarchive fields are completed. The return value is the number of records found.
Scenario 2: More than 1,000 matching records found, request not processed—Failure
You want to unarchive all candidates who have a master's degree.
- You execute the Archive Apex Unarchive function with the filter
has MA=true. - The system searches the candidate list and finds more than 1,000 archived records.
- The Archive Apex unarchive process ends unsuccessfully, and no activity is created. The return value includes the number of records found and an error: 400, scope too large.
Scenario 3: Fewer than 1,000 records, no matching records found, no request processed—Failure
You want to unarchive all candidates who have a master's degree.
- You execute the Archive Apex Unarchive function with the filter
has MA=true. - The system searches the candidate list and finds fewer than 1,000 records, but no matching records are found. No activity is created. The return value is the number of records found.
Scenario 4: Fewer than 1,000 matching records found, request processed, partial success—Success!
You want to unarchive all candidates who have a bachelor's degree.
- You execute the Archive Apex Unarchive function with the filter
has BA=true. - The system searches the candidate list, finds fewer than 1,000 records, and unarchives them.
- Records that match the filter unarchive successfully. A new activity of type Unarchive SDK is added, and all unarchive fields are completed. The return value is the number of records found.
Code Documentation
performUnarchiveSdk(String sObjectName, List<SearchFilter> filters, DateRange dateRange)
performUnarchiveSdk(String sObjectName, List<SearchFilter> filters)Return Value: ArchiverAccessorResponse
ArchiverAccessorResponse.getBody(); ⇒ String
ArchiverAccessorResponse.getStatusCode(); ⇒ integer
200 - if the request was accepted
400 - if the request wasn't accepted
ArchiverAccessorResponse.getErrorMessage(); ⇒ StringCode Examples
Example 1: Unarchive Accounts named Own.
List<SearchFilter> request = new List<SearchFilter>();
String sObjectName = 'Account';
SearchFilter filter1 = new SearchFilter('Name', 'Own');
request.add(filter1);
ArchiverAccessorResponse res = ArchiverAccessor.performUnarchiveSdk(sObjectName, request);
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
system.debug(jsonMap.get('request_id'));
system.debug(jsonMap.get('total_result_count'));Example 2: Unarchive Accounts with a filter, where name is PersonAccount3 and the date range is records created in the last 150 days.
// Initialize a list to hold search filters for the archive operation
List<SF_Archive.SearchFilter> request = new List<SF_Archive.SearchFilter>();
// Define the Salesforce object name to perform the operation on
String sObjectName = 'Account';
// Create a search filter based on the 'Name' field with a specific value
SF_Archive.SearchFilter filter1 = new SF_Archive.SearchFilter('Name', 'PersonAccount3');
// Add the created filter to the request list
request.add(filter1);
// Create a date range filter to select records created in the last 150 days
// It calculates the start date by subtracting 150 days from today's date
SF_Archive.dateRange range = new SF_Archive.dateRange('CreatedDate', system.today()-150, system.today());
// Perform the unarchive operation with the specified object name, filters, and date range
// This retrieves records from the archival that match the given criteria
SF_Archive.ArchiverAccessorResponse res = SF_Archive.ArchiverAccessor.performUnarchiveSdk(sObjectName, request, range);
// Deserialize the JSON response body to a Map object to easily access the data
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
// Debug print the request ID and total result count from the response for logging purposes
system.debug(jsonMap.get('request_id'));
system.debug(jsonMap.get('total_result_count'));- Unarchive Apex Scenarios
Here are examples of the unarchive Apex process in the Archive app.
Unarchive Apex Scenarios
Here are examples of the unarchive Apex process in the Archive app.
Scenario 1: Fewer than 1,000 matching records found, request processed—Success!
According to a new business request, a customer wants to unarchive all candidates who have a bachelor's degree.
-
Use the Archive Apex Unarchive function with the filter
has BA=true. - The candidate list found has fewer than 1,000 unarchived records.
-
A new activity is added with a new typeArchive Apex Unarchive function and
all the unarchive fields filled out.
Return value = The number of records found.
Scenario 2: More than 1,000 matching records found, request not processed—Failure
According to a new business request, the customer wants to unarchive all candidates who have a master's degree.
-
Use the Archive Apex Unarchive function with the filter
has MA=true. - The candidate list found has more than 1,000 unarchived records.
-
The Archive Apex unarchive process ends unsuccessfully.
No activity is created.Return value =# records found, error: 400, scope too large
Scenario 3: Fewer than 1,000 records, no matching records found, no request processed—Failure
According to a new business request, the customer wants to unarchive all candidates who have a master's degree.
-
Use the Archive Apex Unarchive function with the filter
has MA=true. -
The candidate list found has fewer than 1,000 records. No matching records
are found.
No activity is created.Return value = # records found
Scenario 4: Fewer than 1,000 matching records found, request processed, partial success—Success!
According to a new business request, the customer wants to unarchive all candidates who have a bachelor's degree.
-
Use the Archive Apex Unarchive function with the filter
has BA=true. - The candidate list found is fewer than 1,000 records, and the records are unarchived.
-
Records that match the filter unarchive successfully.
A new activity is added with a new type Unarchive-Apex and all the unarchive fields are filled out.
Return value = # records found
