You are here:
Unarchive Apex
In the Archive app, programmatically start an unarchive function based on specific filters. Then initiate a search by code and get the results that match your request.
By unarchiving the record, you unarchive the whole hierarchical tree of any root or child records related to it.
Prerequisites:
Archive Enable Unarchive—Unarchive records from the Archive, or via Apex.
If you need only one unarchive permission, you can enable one of these custom permissions, and then manually assign it to a custom permission set or profile.
- Archive Unarchive—Permission to unarchive records from Archive only.
- Archive Unarchive SDK—Permission to unarchive records via Archive Apex only.
Unarchive Apex Functions
You can search one object per unarchive request and apply a maximum of 6 filters to the search.
1. performUnarchiveSdk(string sObjectName, list<SearchFilter> filters, DateRange dateRange)
2. performUnarchiveSdk(string sObjectName, list<SearchFilter> filters)Apex Parameters
sObjectName—The object queried by Apex.
This parameter is mandatory.
Search Filters—Filters that narrow search results based on field conditions.
- Field Name—The field that you want to filter by, such as
NameorCreatedDate. - Value or List of Values—The specific value or values that you want to match.
If using a list of values, Apex Data Queries are queried with
ORbetween the values.
This parameter is mandatory.
Date Filters—If you want to search based on dates, use the DateRange parameter
and the mandatory filters. DateRange filters records by fields such
as ArchiveDate, CreatedDate, and
ModifiedDate.
Date filter recommendations:
- To search based on the Archive Date, add the
archive_datedate field. - Use the Apex date format: MM/DD/YYYY.
This parameter is optional.
Returned Values
- Request ID of the unarchive operation performed
- Total number of records retrieved by search
- Status code, including errors.
Return Value:
ArchiverAccessorResponse
1. ArchiverAccessorResponse.getBody(); ⇒ string
2. ArchiverAccessorResponse.getStatusCode();⇒ string
a. 200 - if we accepted your request
b. 400 - if we did not accept your request
3. ArchiverAccessorResponse.getErrorMessage();⇒ stringCode Documentation
performUnarchiveSdk(string sObjectName, list<SearchFilter> filters, DateRange dateRange)performUnarchiveSdk(string sObjectName, list<SearchFilter> filters)Return Value: ArchiverAccessorResponse
1. ArchiverAccessorResponse.getBody(); ⇒ string
2. ArchiverAccessorResponse.getStatusCode();⇒ string
a. 200 - if we accepted your request
b. 400 - if we did not accept your request
3. ArchiverAccessorResponse.getErrorMessage();⇒ stringApex Unarchive Code Example
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

