ステップ 4: クラスメソッドの最終バージョンを作成する
計画を作成できたら、疑似コードを補足して最終的なクラスメソッドのロジックを構築しましょう。まず、対象の請求書とその品目の sObject のローカルコピーを作成します (下記の行 8 ~ 10)。メソッドのコードには、既存の品目を順序どおりに並べる SOQL クエリが含まれ (行 8)、メソッドの入力パラメータの指定に基づいて (行 10)、検索条件を使用し、対象の請求書を取得します。SOQL のオブジェクト表記は、他とは少し異なります。
ソースコードについては、https://gist.github.com/3605645を参照してください。
1public class InvoiceUtilities {
2 // Class method to renumber Line Items for a given Invoice number.
3 // Returns a string that indicates success or failure.
4 public static String renumberLineItems(String invoiceName) {
5
6 // Create a copy of the target Invoice object and its Line Items.
7 Invoice__c invoice =
8 [SELECT i.Name, (Select Name FROM Line_Items__r ORDER BY Name)
9 FROM Invoice__c i
10 WHERE i.Name = :invoiceName LIMIT 1];
11
12 // Loop through each Line Item, renumbering as you go.
13
14 // Update the Line Items in one transaction, rollback if any problems
15 // and return error messages to the calling environment.
16
17 // On success, return a message to the calling program.
18 return 'Line items renumbered successfully.';
19 }
20}更新された Apex クラスを保存します。保存操作をするときには、[Problems (問題)] ペインでコンパイルエラーがないことを確認します。エラーがある場合は、適切に修正して、修正後のコードを保存します。
次に、各品目を処理して採番し直すためのループを追加してクラスを更新します (行 13 ~ 18)。
ソースコードは、https://gist.github.com/3605650 にあります。
1public class InvoiceUtilities {
2
3 // Class method to renumber Line Items for a given Invoice number.
4 // Returns a string that indicates success or failure.
5 public static String renumberLineItems(String invoiceName) {
6
7 // Create a copy of the target Invoice object and its Line Items.
8 Invoice__c invoice =
9 [SELECT i.Name, (SELECT Name FROM Line_Items__r ORDER BY Name)
10 FROM Invoice__c i
11 WHERE i.Name = :invoiceName LIMIT 1];
12
13 // Loop through each Line Item, renumbering as you go.
14 Integer i = 1;
15 for (Line_Item__c item : invoice.Line_Items__r) {
16 item.Name = String.valueOf(i);
17 System.debug(item.Name);
18 i++;
19 }
20
21 // Update the Line Items in one transaction, rollback if any problems,
22 // and return error messages to the calling environment.
23
24 // On success, return a message to the calling program.
25 return 'Line items renumbered successfully.';
26 }
27}行 14 の FOR ループでは、Apex 固有のオブジェクト表記を使用して請求書の品目を参照しています。行 18 には、デバッグログに役に立つ情報を出力するための System.debug ステートメントが含まれています。
次に、新しいバージョンの請求書の品目でデータベースを更新する、クラスメソッドの最終バージョンを作成します (行 22 ~ 30)。
ソースコードについては、https://gist.github.com/3605654を参照してください。
1public class InvoiceUtilities {
2
3 // Class method to renumber Line Items for a given Invoice number.
4 // Returns a string that indicates success or failure.
5 public static String renumberLineItems(String invoiceName) {
6
7 // Create a copy of the target Invoice object and its Line Items.
8 Invoice__c invoice =
9 [SELECT i.Name, (SELECT Name FROM Line_Items__r ORDER BY Name)
10 FROM Invoice__c i
11 WHERE i.Name = :invoiceName LIMIT 1];
12
13 // Loop through each Line Item, renumbering as you go.
14 Integer i = 1;
15 for (Line_Item__c item : invoice.Line_Items__r) {
16 item.Name = String.valueOf(i);
17 System.debug(item.Name);
18 i++;
19 }
20
21 // Update the Line Items in one transaction, rollback if any problems
22 // and return error messages to the calling environment.
23 try {
24 Database.update(invoice.Line_Items__r);
25 }
26 catch (DmlException e) {
27 return e.getMessage();
28 }
29
30 // On success, return a message to the calling program.
31 return 'Line items re-numbered successfully.';
32 }
33}このメソッドは、try/catch ブロックを使用してデータベースを更新し、予期しないランタイム例外が発生した場合はエラーを処理します。
- try ブロック (行 22 ~ 24) の Database.update メソッドは標準の Apex メソッドで、1 つ以上の sObject を更新するために使用できます。ここでも、対象となる請求書の関連する品目を参照するオブジェクト表記に注意してください (invoice.Line_Items__r)。
- catch ブロック (行 25 ~ 27) は、DmlException をキャッチします。例外エラーメッセージを呼び出し元に返す return ステートメントが含まれています。
- メソッドは、try/catch ブロックを通過して処理を��行する (つまり、例外が発生せず、例外エラーメッセージを返さない) と、標準のメッセージを返して処理が成功したことを示します (行 30)。