この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Apex REST の基本コードサンプル

このサンプルでは、3 つの異なる HTTP 要求メソッドを処理する簡単な REST APIApex に実装する方法を示します。cURL を使った認証についての詳細は、『REST API Developer's Guide』の「クイックスタート」のセクションを参照してください。
  1. [設定] から [開発] | [Apex クラス] | [新規] をクリックし、このコードを新しいクラスに追加して、インスタンスに Apex クラスを作成します。
    1swfobject.registerObject("clippy.codeblock-0", "9");
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17@RestResource(urlMapping='/Account/*')
    18global with sharing class MyRestResource {
    19
    20    @HttpDelete
    21    global static void doDelete() {
    22        RestRequest req = RestContext.request;
    23        RestResponse res = RestContext.response;
    24        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    25        Account account = [SELECT Id FROM Account WHERE Id = :accountId];
    26        delete account;
    27    }
    28  
    29    @HttpGet
    30    global static Account doGet() {
    31        RestRequest req = RestContext.request;
    32        RestResponse res = RestContext.response;
    33        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    34        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
    35        return result;
    36    }
    37  
    38  @HttpPost
    39    global static String doPost(String name,
    40        String phone, String website) {
    41        Account account = new Account();
    42        account.Name = name;
    43        account.phone = phone;
    44        account.website = website;
    45        insert account;
    46        return account.Id;
    47    }
    48}
  2. クライアントから doGet メソッドをコールするには、コマンドラインウィンドウを開き、次の cURL コマンドを実行して ID で取引先を取得します。
    curl -H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"
    • sessionId を、ログイン応答でメモした <sessionId> 要素に置き換えます。
    • instance<serverUrl> 要素に置き換えます。
    • accountId を、組織に存在する取引先の ID に置き換えます。

    doGet メソッドをコールすると、Salesforce が次のようなデータを伴う JSON 応答を返します。

    1{
    2  "attributes" : 
    3    {
    4      "type" : "Account",
    5      "url" : "/services/data/v22.0/sobjects/Account/accountId"
    6    },
    7  "Id" : "accountId",
    8  "Name" : "Acme"
    9
    10}

    このセクションの cURL の例では、名前空間による Apex クラスを使用していないため、URL に名前空間は含まれません。

    メモ

  3. 次のステップで作成する取引先のデータを含めるための account.txt というファイルを作成します。
    1{
    2  "name" : "Wingo Ducks",
    3  "phone" : "707-555-1234",
    4  "website" : "www.wingo.ca.us"
    5}
  4. コマンドラインウィンドウを使用して、次の cURL コマンドを実行し、新しい取引先を作成します。

    curl -H "Authorization: Bearer sessionId" -H "Content-Type: application/json" -d @account.txt "https://instance.salesforce.com/services/apexrest/Account/"

    doPost メソッドをコールすると、Salesforce が次のようなデータを伴う応答を返します。

    1"accountId"

    accountId は、POST 要求で作成した取引先の ID です。

  5. コマンドラインウィンドウを使用して、次の cURL コマンドを実行し、ID の指定によって取引先を削除します。

    curl —X DELETE —H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"