The undelete DML operation
restores one or more
existing sObject records, such as individual accounts or contacts,
from your organization’s Recycle Bin.undelete is analogous to the UNDELETE statement in SQL.
The optional opt_allOrNoneparameter specifies
whether the operation allows partial success. If you specify false for this parameter and a record
fails, the remainder of the DML operation can still succeed. This
method returns a result object that can be used to verify which records
succeeded, which failed, and why.
Rules and Guidelines
When undeleting sObject
records, consider the following rules and guidelines:
To ensure referential integrity, undelete restores the record associations for the following
types of relationships:
Parent accounts (as specified in the Parent Account field on an account)
Parent cases (as specified in the Parent Case field on a case)
Master solutions for translated solutions (as specified in the Master Solution field on a solution)
Managers of contacts (as specified in the Reports To field on a contact)
Products related to assets (as specified in the Product field on an asset)
Opportunities related to quotes (as specified in the Opportunity field on a quote)
All custom lookup relationships
Relationship group members on accounts and relationship groups,
with some exceptions
Tags
An article's categories, publication state, and assignments
Certain sObjects can't be undeleted. To verify if an sObject record
can be undeleted, check that the undeletable property of the sObject is set to true.
You can pass a maximum of 10,000 sObject records to a single undelete method.
You can undelete records that were deleted as the result of a
merge, but the child objects will have been re-parented, which cannot
be undone.
Use the ALL ROWS parameters
with a SOQL query to identify deleted records, including records deleted
as a result of a merge. See Querying All Records with a SOQL Statement.
An
array of Database.UndeleteResult objects is returned with the undelete database method. Each element
in the UndeleteResult array corresponds to the sObject array passed
as the sObject[] parameter in the undelete database method, that is,
the first element in the UndeleteResult array matches the first element
passed in the sObject array, the second element corresponds with the
second element, and so on. If only one sObject is passed in, the UndeleteResults
array contains a single element.
An undeleteResult object has
the following methods:
If an error occurred, an array of one or more database error
objects providing the error code and description. For more information,
see Database Error Object Methods.
getId
ID
The ID of the sObject you were trying to undelete. If this
field contains a value, the object was successfully undeleted. If
this field is empty, the operation was not successful for that object.
isSuccess
Boolean
A Boolean value that is set to true if the DML operation was
successful for this object, false otherwise
DML Statement Example
The following
example undeletes an account named 'Trump'. The ALL ROWS keyword queries all rows
for both top level and aggregate relationships, including deleted
records and archived activities.
Account a = new Account(Name='AC1');
insert(a);
insert(new Contact(LastName='Carter',AccountId=a.Id));
Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Trump' ALL ROWS];
try {
undelete savedAccts;
} catch (DmlException e) {
// Process exception here
}
Database Method Example
The following
example undeletes an account named 'Trump'. The ALL ROWS keyword queries all rows
for both top level and aggregate relationships, including deleted
records and archived activities.
publicclass DmlTest2 {
publicvoid undeleteExample() {
Account[] SavedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Trump' ALL ROWS];
Database.UndeleteResult[] UDR_Dels = Database.undelete(SavedAccts);
for(integer i =0; i< 10; i++)
if(UDR_Dels[i].getErrors().size()>0){
// Process any errors here
}
}
}