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

CRUD ベースのメタデータ開発

組織またはアプリケーションの設定・定義コンポーネントを作成、更新、または削除するには、CRUD ベースのメタデータコールを使用します。これらの設定コンポーネントには、カスタムオブジェクト、カスタム項目、およびその他の設定メタデータが含まれます。メタデータコールは、コンポーネントの作成、更新、または削除について、Salesforce ユーザインターフェースの動作を模倣します。適用されるすべてのルールは、これらのコールにも適用されます。

メタデータコールは、コアの同期 API コールとは次の点で異なります。
  • メタデータ API コールは、別の WSDL で使用できます。WSDL をダウンロードするには、Salesforce にログインし、[設定] から [クイック検索] ボックスに「API」と入力し、[API] を選択して、[メタデータ WSDL のダウンロード] リンクをクリックします。
  • ログイン後、SOAP API 以外の URL を持つメタデータ API エンドポイントにメタデータ API コールを送信する必要があります。SOAP API login() コールによって返される LoginResult から metadataServerUrl を取得します。SOAP API についての詳細は、SOAP API 開発者ガイドを参照してください。
  • メタデータコールには、同期と非同期があります。API バージョン 30.0 以降では、CRUD コールは同期で、API コアコールと同様に 1 つのコールで結果が返されます。以前の API バージョンでは、作成、更新、および削除は非同期のみであるため、結果は 1 つのコールですぐに返されません。
  • 対応するコアの SOAP API 同期コールに対応付けられた同期メタデータコールがあります。
    • createMetadata() は、create() SOAP API コールに対応付けられます。
    • updateMetadata() は、update() SOAP API コールに対応付けられます。
    • deleteMetadata() は、delete() SOAP API コールに対応付けられます。

メタデータ API は、メタデータコンポーネントの取得とリリースを行う retrieve() コールと deploy() コールもサポートしています。詳細は、「メタデータのリリースと取得」を参照してください。

メモ

同期コールを使用した CRUD ベース開発用の Java サンプル

このセクションでは、CRUD ベースのコールを使用する Java クライアントアプリケーションのサンプルについて説明します。このサンプルアプリケーションでは、次の主要なタスクを実行します。

  1. MetadataLoginUtil.java クラスを使用し、Metadata 接続を作成します。詳細は、「ステップ 3: Java サンプルコードの説明」を参照してください。
  2. カスタムオブジェクトを作成するには、createMetadata() をコールします。このコールは、1 つのコールで結果を返します。
  3. 返される SaveResult オブジェクトを調べて、操作に成功したかどうかを確認し、成功していなかった場合はコンポーネント名、エラーメッセージ、および状況コードを出力に書き込みます。
1import com.sforce.soap.metadata.*;
2
3/**
4 * Sample that logs in and creates a custom object through the metadata API
5 */
6public class CRUDSampleCreate {
7    private MetadataConnection metadataConnection;
8
9    // one second in milliseconds
10    private static final long ONE_SECOND = 1000;
11
12    public CRUDSampleCreate() {
13    }
14
15    public static void main(String[] args) throws Exception {
16        CRUDSampleCreate crudSample = new CRUDSampleCreate();
17        crudSample.runCreate();
18    }
19
20    /**
21     * Create a custom object. This method demonstrates usage of the
22     * create() and checkStatus() calls.
23     *
24     * @param uniqueName Custom object name should be unique.
25     */
26    private void createCustomObjectSync(final String uniqueName) throws Exception {
27        final String label = "My Custom Object";
28        CustomObject co = new CustomObject();
29        co.setFullName(uniqueName);
30        co.setDeploymentStatus(DeploymentStatus.Deployed);
31        co.setDescription("Created by the Metadata API Sample");
32        co.setEnableActivities(true);
33        co.setLabel(label);
34        co.setPluralLabel(label + "s");
35        co.setSharingModel(SharingModel.ReadWrite);
36
37        // The name field appears in page layouts, related lists, and elsewhere.
38        CustomField nf = new CustomField();
39        nf.setType(FieldType.Text);
40        nf.setDescription("The custom object identifier on page layouts, related lists etc");
41        nf.setLabel(label);
42        nf.setFullName(uniqueName);
43        customObject.setNameField(nf);
44
45        SaveResult[] results = metadataConnection
46                .createMetadata(new Metadata[] { co });
47
48        for (SaveResult r : results) {
49            if (r.isSuccess()) {
50                System.out.println("Created component: " + r.getFullName());
51            } else {
52                System.out
53                        .println("Errors were encountered while creating "
54                                + r.getFullName());
55                for (Error e : r.getErrors()) {
56                    System.out.println("Error message: " + e.getMessage());
57                    System.out.println("Status code: " + e.getStatusCode());
58                }
59            }
60        }
61    }
62
63    private void runCreate() throws Exception {
64        metadataConnection = MetadataLoginUtil.login();
65        // Custom objects and fields must have __c suffix in the full name.
66        final String uniqueObjectName = "MyCustomObject__c";
67        createCustomObjectSync(uniqueObjectName);
68    }
69}

非同期コールを使用した CRUD ベース開発用の Java サンプル

このセクションのサンプルでは、非同期 create() CRUD コールを使用します。非同期 CRUD コールは API バージョン 31.0 で使用できなくなりました。それより前の API バージョンでのみ使用できます。

重要

このセクションでは、非同期 CRUD ベースのコールを使用する Java クライアントアプリケーションのサンプルについて説明します。このサンプルアプリケーションでは、次の主要なタスクを実行します。

  1. MetadataLoginUtil.java クラスを使用し、Metadata 接続を作成します。詳細は、「ステップ 3: Java サンプルコードの説明」を参照してください。
  2. 新しいカスタムオブジェクトを作成するには、create() をコールします。

    Salesforce では、作成しようとしたコンポーネントごとに AsyncResult オブジェクトが返されます。AsyncResult オブジェクトは、操作がキューから完了またはエラー状態に移行するたびに、状況情報で更新されます。

  3. AsyncResult の状況値が create 操作が完了したことを示すまで、ループで checkStatus() をコールします。

API コールに続く、エラー処理コードに注意してください。

1import com.sforce.soap.metadata.*;
2
3/**
4 * Sample that logs in and creates a custom object through the metadata api
5 */
6public class CRUDSample {
7    private MetadataConnection metadataConnection;
8
9    // one second in milliseconds
10    private static final long ONE_SECOND = 1000;
11
12    public CRUDSample() {
13    }
14
15    public static void main(String[] args) throws Exception {
16        CRUDSample crudSample = new CRUDSample();
17        crudSample.runCreate();
18    }
19
20    /**
21     * Create a custom object. This method demonstrates usage of the
22     * create() and checkStatus() calls.
23     *
24     * @param uniqueName Custom object name should be unique.
25     */
26    private void createCustomObject(final String uniqueName) throws Exception {
27        final String label = "My Custom Object";
28        CustomObject customObject = new CustomObject();
29        customObject.setFullName(uniqueName);
30        customObject.setDeploymentStatus(DeploymentStatus.Deployed);
31        customObject.setDescription("Created by the Metadata API Sample");
32        customObject.setLabel(label);
33        customObject.setPluralLabel(label + "s");
34        customObject.setSharingModel(SharingModel.ReadWrite);
35
36        // The name field appears in page layouts, related lists, and elsewhere.
37        CustomField nf = new CustomField();
38        nf.setType(FieldType.Text);
39        nf.setDescription("The custom object identifier on page layouts, related lists etc");
40        nf.setLabel(label);
41        nf.setFullName(uniqueName);
42        customObject.setNameField(nf);
43
44        AsyncResult[] asyncResults = metadataConnection.create(
45            new CustomObject[]{customObject});
46        if (asyncResults == null) {
47            System.out.println("The object was not created successfully");
48            return;
49        }
50
51        long waitTimeMilliSecs = ONE_SECOND;
52
53        // After the create() call completes, we must poll the results of the checkStatus()
54        // call until it indicates that the create operation has completed.
55        do {
56            printAsyncResultStatus(asyncResults);
57            waitTimeMilliSecs *= 2;
58            Thread.sleep(waitTimeMilliSecs);
59            asyncResults = metadataConnection.checkStatus(new String[]{asyncResults[0].getId()});
60        } while (!asyncResults[0].isDone());
61
62        printAsyncResultStatus(asyncResults);
63    }
64
65    private void printAsyncResultStatus(AsyncResult[] asyncResults) throws Exception {
66        if (asyncResults == null || asyncResults.length == 0 || asyncResults[0] == null) {
67            throw new Exception("The object status cannot be retrieved");
68        }
69
70        AsyncResult asyncResult = asyncResults[0]; //we are creating only 1 metadata object
71
72        if (asyncResult.getStatusCode() != null) {
73            System.out.println("Error status code: " +
74                    asyncResult.getStatusCode());
75            System.out.println("Error message: " + asyncResult.getMessage());
76        }
77
78        System.out.println("Object with id:" + asyncResult.getId() + " is " +
79            asyncResult.getState());
80    }
81
82    private void runCreate() throws Exception {
83        metadataConnection = MetadataLoginUtil.login();
84        // Custom objects and fields must have __c suffix in the full name.
85        final String uniqueObjectName = "MyCustomObject__c";
86        createCustomObject(uniqueObjectName);
87    }
88}