Salesforce RemoteAction and webService

Salesforceで、Visualforce Pageから非同期でデータ取得したい場合、RemoteActionまたはwebServiceがよく考えられます。

本PostにはRemoteActionとwebServiceの使用方法及び差異を共有したいと思います。

RemoteAction

RemoteActionからApexクラスのメソッドの呼び出して、リターンデータの取得が可能です。(@RemoteActionアノテーションと<apex:actionFunction>の差異はこちらご参照)
@RemoteActionアノテーションメソッドはstaticとglobalかpublicに宣言しなければなりません。
下記はRemoteActionのサンプルコードです。

RemoteActionDemo.page

RemoteActionDemoCls.cls

Response

webService

webService(Apex in AJAX)を使用する場合、まず下記のAjax Toolkitライブラリをインポートする必要があります。 webServiceメソッドはstaticに宣言し、親Apexクラスはglobalで宣言しなければなりません。
次はwebServiceのサンプルコードです。

WebServiceDemo.page

WebServiceDemoCls.cls

Response

RemoteActionとwebServiceの差異

1. webServiceはPageコントローラだけではなく、他のApexクラスにも呼び出す可能です。従って、呼び出し先は共通化可能です。
2. webServiceを使用する場合、API請求数が消費されます。RemoteActionなら消費しません。
3. webServiceを使用する場合、親Apexクラスはglobalで宣言しなければなりません。