Newer Version Available

This content describes an older version of this product. View Latest

undelete()

Undeletes records from the Recycle Bin.

Syntax

1UndeleteResult[] = connection.undelete(ID[] ids );

Usage

Use this call to restore any deleted record that is undeletable. Undeletable records include those in the Recycle Bin. Records can be put in the Recycle Bin as the result of a merge() or delete() call. You can identify deleted records, including records deleted as the result of a merge, using the queryAll() call.

You should verify that a record can be undeleted before attempting to delete it. Some records cannot be undeleted, for example, Account records can be undeleted, but not AccountTeamMember records. To verify that a record can be undeleted, check that the value of the undeletable flag in the DescribeSObjectResult for that object is set to true.

Since a delete call cascade-deletes child records, an undelete call will undelete the cascade-deleted records. For example, deleting an account will delete all the contacts associated with that account.

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.

Starting with API version 15.0, if you specify a value for a field that contains a string, and the value is too big for the field, the call fails and an error is returned. In previous versions of the API the value was truncated and the call succeeded. If you wish to keep the old behavior with versions 15.0 and later, use the AllowFieldTruncationHeader SOAP header.

Note

This call supports the AllOrNoneHeader, AllowFieldTruncationHeader, and CallOptions headers.

Rollback on Error

The AllOrNoneHeader header allows you to roll back all changes unless all records are processed successfully. This header is available in API version 20.0 and later. The default behavior is to allow partial success of a call: records without errors are committed, while records with errors are marked as failed in the call results.

Sample Code—Java

This sample calls queryAll() to get the last five deleted accounts. It then passes the IDs of these accounts to undelete(), which restores these accounts. Finally, it checks the results of the call and writes the IDs of the restored accounts or any errors to the console.

1swfobject.registerObject("clippy.codeblock-1", "9");public void undeleteRecords() {
2   try {
3      // Get the accounts that were last deleted
4      // (up to 5 accounts)
5      QueryResult qResult = connection
6            .queryAll("SELECT Id, SystemModstamp FROM "
7                  + "Account WHERE IsDeleted=true "
8                  + "ORDER BY SystemModstamp DESC LIMIT 5");
9
10      String[] Ids = new String[qResult.getSize()];
11      // Get the IDs of the deleted records
12      for (int i = 0; i < qResult.getSize(); i++) {
13         Ids[i] = qResult.getRecords()[i].getId();
14      }
15
16      // Restore the records
17      UndeleteResult[] undelResults = connection.undelete(Ids);
18
19      // Check the results
20      for (UndeleteResult result : undelResults) {
21         if (result.isSuccess()) {
22            System.out.println("Undeleted Account ID: " + result.getId());
23         } else {
24            if (result.getErrors().length > 0) {
25               System.out.println("Error message: "
26                     + result.getErrors()[0].getMessage());
27            }
28         }
29      }
30   } catch (ConnectionException ce) {
31      ce.printStackTrace();
32   }
33}

Sample Code—C#

This sample calls queryAll() to get the last five deleted accounts. It then passes the IDs of these accounts to undelete(), which restores these accounts. Finally, it checks the results of the call and writes the IDs of the restored accounts or any errors to the console.

1swfobject.registerObject("clippy.codeblock-2", "9");public void undeleteRecords()
2{
3   try
4   {
5      // Get the accounts that were last deleted
6      //   (up to 5 accounts)
7      QueryResult qResult = binding.queryAll(
8            "SELECT Id, SystemModstamp FROM " +
9            "Account WHERE IsDeleted=true " +
10            "ORDER BY SystemModstamp DESC LIMIT 5");
11
12      String[] Ids = new String[qResult.size];
13      // Get the IDs of the deleted records
14      for (int i = 0; i < qResult.size; i++)
15      {
16         Ids[i] = qResult.records[i].Id;
17      }
18
19      // Restore the records
20      UndeleteResult[] undelResults = binding.undelete(Ids);
21
22      // Check the results
23      foreach (UndeleteResult result in undelResults)
24      {
25         if (result.success)
26         {
27            Console.WriteLine("Undeleted Account ID: " +
28                  result.id);
29         }
30         else
31         {
32            if (result.errors.Length > 0)
33            {
34               Console.WriteLine("Error message: " +
35                     result.errors[0].message);
36            }
37         }
38      }
39   }
40   catch (SoapException e)
41   {
42      Console.WriteLine("An unexpected error has occurred: " +
43                                 e.Message + "\n" + e.StackTrace);
44   }
45}

Arguments

Name Type Description
ids ID[] IDs of the records to be restored.

Response

UndeleteResult