ステップ 3: Apex クラスを変更する
カスタムボタンの JavaScript は、AJAX Toolkit を利用して JavaScript から SOAP コールを行います。そのため、SOAP API コールをサポートするためには Apex クラスおよびメソッドに 2 つの軽微な変更を加える必要があります。
ブラウザ内で Apex クラスを変更する手順は、次のとおりです。
- [設定] から、[クイック検索] ボックスに「Apex クラス」と入力し、[Apex クラス] を選択します。
- InvoiceUtilities クラスの横に、[編集]、[削除] および [セキュリティ] の 3 つのリンクがあります。[編集] をクリックします。
- クラスとメソッドの両方の範囲を、それぞれ public から global および webservice (行 1 および行 5) に変更し、[保存] をクリックします。
ソースコードについては、https://gist.github.com/3605663を参照してください。
最終的なコードは次のようになります。
1global with sharing class InvoiceUtilities {
2 // Class method to renumber Line Items for a given Invoice number.
3 // Returns a string that indicates success or failure.
4 webservice 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 Integer i = 1;
14 for (Line_Item__c item : invoice.Line_Items__r) {
15 item.Name = String.valueOf(i);
16 System.debug(item.Name);
17 i++;
18 }
19
20 // Update the Line Items in one transaction, rollback if any problems
21 // and return error messages to the calling environment.
22 try {
23 Database.update(invoice.Line_Items__r);
24 }
25 catch (DmlException e) {
26 return e.getMessage();
27 }
28
29 // On success, return a message to the calling program.
30 return 'Line items renumbered successfully.';
31 }
32}Apex クラスのリストに戻ると、InvoiceUtilities クラスに WSDL という新しいリンクがあります。このリンクをクリックすると、アプリケーションがこのクラスへのインターフェースとして使用できる WSDL ファイルが表示されます。