undelete()
ごみ箱からレコードを復元します。
構文
1UndeleteResult[] = connection.undelete(ID[] ids );使用方法
このコールを使用して、削除済みのレコードの中で復元可能なレコードを復元します。復元可能なレコードには、ごみ箱の中にあるレコードも含まれます。レコードは、merge() または delete() コールの結果として、ごみ箱に入れられる場合があります。queryAll() コールを使用して、結合の結果として削除されたレコードなど、削除されたレコードを識別することができます。
削除する前に、レコードが復元できることを確認する必要があります。復元できないレコードもあります。たとえば、Account レコードは復元できますが、AccountTeamMember レコードは復元��きません。オブジェクトが復元できることを確認するには、そのオブジェクトの DescribeSObjectResult の undeletable フラグの値が true に設定されていることを確認します。
delete コールは、子レコードをカスケード削除しますが、undelete コールは、カスケード削除されたレコードを復元します。たとえば、取引先を削除すると、その取引先に関連するすべての取引先責任者を削除します。
結合の結果として削除されたレコードを復元できますが、子オブジェクトに再設定された親を元に戻すことはできません。
このコールは、AllOrNoneHeader、AllowFieldTruncationHeader、および CallOptions ヘッダーをサポートしています。
エラー時のロールバック
AllOrNoneHeader ヘッダーを使用すると、すべてのレコードが正常に処理されない限り、すべての変更をロールバックできます。このヘッダーは、API バージョン 20.0 以降で使用できます。正常に処理されないレコードがあった場合に、コールですべての変更をロールバックできます。
サンプルコード —Java
このサンプルでは、queryAll() をコールして、直近に削除された取引先を 5 つ取得します。次に、これらの取引先の ID を undelete() に渡し、これらの取引先を復元します。最後に、コールの結果を確認して、復元された取引先の ID またはエラーをコンソールに書き込みます。
1public 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}サンプルコード —C#
このサンプルでは、queryAll() をコールして、直近に削除された取引先を 5 つ取得します。次に、これらの取引先の ID を undelete() に渡し、これらの取引先を復元します。最後に、コールの結果を確認して、復元された取引先の ID またはエラーをコンソールに書き込みます。
1public 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}引数
| 名前 | 型 | 説明 |
|---|---|---|
| ids | ID[] | 復元するレコードの ID。 |