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

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

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

メタデータコールは、コアの同期 API コールとは次の点で異なります。
  • メタデータ API コールは、別の WSDL で使用できます。WSDL をダウンロードするには、Salesforce にログインし、[設定] で [開発] | [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 オブジェクトを調べて、操作に成功したかどうかを確認し、成功していなかった場合はコンポーネント名、エラーメッセージ、および状況コードを出力に書き込みます。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import com.sforce.soap.metadata.*;
18
19/**
20 * Sample that logs in and creates a custom object through the metadata API
21 */
22public class CRUDSampleCreate {
23    private MetadataConnection metadataConnection;
24
25    // one second in milliseconds
26    private static final long ONE_SECOND = 1000;
27
28    public CRUDSampleCreate() {
29    }
30
31    public static void main(String[] args) throws Exception {
32        CRUDSampleCreate crudSample = new CRUDSampleCreate();
33        crudSample.runCreate();
34    }
35
36    /**
37     * Create a custom object. This method demonstrates usage of the
38     * create() and checkStatus() calls.
39     *
40     * @param uniqueName Custom object name should be unique.
41     */
42    private void createCustomObjectSync(final String uniqueName) throws Exception {
43        final String label = "My Custom Object";
44        CustomObject co = new CustomObject();
45        co.setFullName(uniqueName);
46        co.setDeploymentStatus(DeploymentStatus.Deployed);
47        co.setDescription("Created by the Metadata API Sample");
48        co.setEnableActivities(true);
49        co.setLabel(label);
50        co.setPluralLabel(label + "s");
51        co.setSharingModel(SharingModel.ReadWrite);
52
53        // The name field appears in page layouts, related lists, and elsewhere.
54        CustomField nf = new CustomField();
55        nf.setType(FieldType.Text);
56        nf.setDescription("The custom object identifier on page layouts, related lists etc");
57        nf.setLabel(label);
58        nf.setFullName(uniqueName);
59        customObject.setNameField(nf);
60
61        SaveResult[] results = metadataConnection
62                .createMetadata(new Metadata[] { co });
63
64        for (SaveResult r : results) {
65            if (r.isSuccess()) {
66                System.out.println("Created component: " + r.getFullName());
67            } else {
68                System.out
69                        .println("Errors were encountered while creating "
70                                + r.getFullName());
71                for (Error e : r.getErrors()) {
72                    System.out.println("Error message: " + e.getMessage());
73                    System.out.println("Status code: " + e.getStatusCode());
74                }
75            }
76        }
77    }
78
79    private void runCreate() throws Exception {
80        metadataConnection = MetadataLoginUtil.login();
81        // Custom objects and fields must have __c suffix in the full name.
82        final String uniqueObjectName = "MyCustomObject__c";
83        createCustomObjectSync(uniqueObjectName);
84    }
85}

非同期コールを使用した 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 コールに続く、エラー処理コードに注意してください。

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