Composite Graph の使用
次の例では、Composite Graph を使用する方法を示します。また、1 つの要求で複数の Composite Graph を使用できる方法も示します。
-
通常の Composite 要求では、一連の REST API 要求を単一のコールを実行できます。また、1 つの要求の出力を、後続の要求の入力として使用できます。
-
これは、Composite Graph がより複雑で完全な一連の関連オブジェクトおよびレコードを組み合わせることによって拡張されます。
-
また、Composite Graph では、特定の一連の操作のステップがすべて完了または終了していることが保証されます。これによって、成功と失敗が混在した結果を確認する必要がなくなりました。
-
通常の Composite 要求では、サブ要求の数は 25 に制限されています。Composite Graph はこの制限を 500 に拡大します。これによって、単一の API コールにはるかに大きな能力が与えられます。
要求を作成します。
1curl --request POST \
2--header "Authorization: Bearer token" \
3--header "Content-Type: application/json" \
4--data @data.json \
5instance.salesforce.com/services/data/vXX.X/composite/graphここで、data.json ファイルにはグラフの JSON 定義が含まれます。JSON の一般的な形式を次に示します。
1{
2 "graphs": [
3 {
4 "graphId": "graphId",
5 "compositeRequest": [
6 compositeSubrequest,
7 compositeSubrequest,
8 ...
9 ]
10 },
11 {
12 "graphId": "graphId",
13 "compositeRequest": [
14 compositeSubrequest,
15 compositeSubrequest,
16 ...
17 ]
18 },
19 ...
20 ]
21}ここで、compositeSubrequestResult は Composite サブ要求の結果です。
たとえば、2 つの Composite Graph 要求でそれぞれ取引先を作成してから、関連レコードを作成します。
1{
2 "graphs" : [
3 {
4 "graphId" : "1",
5 "compositeRequest" : [
6 {
7 "url" : "/services/data/v50.0/sobjects/Account/",
8 "body" : {
9 "name" : "Cloudy Consulting"
10 },
11 "method" : "POST",
12 "referenceId" : "reference_id_account_1"
13 },
14 {
15 "url" : "/services/data/v50.0/sobjects/Contact/",
16 "body" : {
17 "FirstName" : "Nellie",
18 "LastName" : "Cashman",
19 "AccountId" : "@{reference_id_account_1.id}"
20 },
21 "method" : "POST",
22 "referenceId" : "reference_id_contact_1"
23 },
24 {
25 "url" : "/services/data/v50.0/sobjects/Opportunity/",
26 "body" : {
27 "CloseDate" : "2024-05-22",
28 "StageName" : "Prospecting",
29 "Name" : "Opportunity 1",
30 "AccountId" : "@{reference_id_account_1.id}"
31 },
32 "method" : "POST",
33 "referenceId" : "reference_id_opportunity_1"
34 }
35 ]
36 },
37 {
38 "graphId" : "2",
39 "compositeRequest" : [
40 {
41 "url" : "/services/data/v50.0/sobjects/Account/",
42 "body" : {
43 "name" : "Easy Spaces"
44 },
45 "method" : "POST",
46 "referenceId" : "reference_id_account_2"
47 },
48 {
49 "url" : "/services/data/v50.0/sobjects/Contact/",
50 "body" : {
51 "FirstName" : "Charlie",
52 "LastName" : "Dawson",
53 "AccountId" : "@{reference_id_account_2.id}"
54 },
55 "method" : "POST",
56 "referenceId" : "reference_id_contact_2"
57 }
58 ]
59 }
60 ]
61}応答は次のようになります。
1{
2 "graphs" : [
3 {
4 "graphId" : "1",
5 "graphResponse" : {
6 "compositeResponse" : [
7 {
8 "body" : {
9 "id" : "001R00000064wc7IAA",
10 "success" : true,
11 "errors" : [ ]
12 },
13 "httpHeaders" : {
14 "Location" : "/services/data/v50.0/sobjects/Account/001R00000064wc7IAA"
15 },
16 "httpStatusCode" : 201,
17 "referenceId" : "reference_id_account_1"
18 },
19 {
20 "body" : {
21 "id" : "003R000000DDMlTIAX",
22 "success" : true,
23 "errors" : [ ]
24 },
25 "httpHeaders" : {
26 "Location" : "/services/data/v50.0/sobjects/Contact/003R000000DDMlTIAX"
27 },
28 "httpStatusCode" : 201,
29 "referenceId" : "reference_id_contact_1"
30 },
31 {
32 "body" : {
33 "id" : "006R0000003FPYxIAO",
34 "success" : true,
35 "errors" : [ ]
36 },
37 "httpHeaders" : {
38 "Location" : "/services/data/v50.0/sobjects/Opportunity/006R0000003FPYxIAO"
39 },
40 "httpStatusCode" : 201,
41 "referenceId" : "reference_id_opportunity_1"
42 }
43 ]
44 },
45 "isSuccessful" : true
46 },
47 {
48 "graphId" : "2",
49 "graphResponse" : {
50 "compositeResponse" : [
51 {
52 "body" : {
53 "id" : "001R00000064wc8IAA",
54 "success" : true,
55 "errors" : [ ]
56 },
57 "httpHeaders" : {
58 "Location" : "/services/data/v50.0/sobjects/Account/001R00000064wc8IAA"
59 },
60 "httpStatusCode" : 201,
61 "referenceId" : "reference_id_account_2"
62 },
63 {
64 "body" : {
65 "id" : "003R000000DDMlUIAX",
66 "success" : true,
67 "errors" : [ ]
68 },
69 "httpHeaders" : {
70 "Location" : "/services/data/v50.0/sobjects/Contact/003R000000DDMlUIAX"
71 },
72 "httpStatusCode" : 201,
73 "referenceId" : "reference_id_contact_2"
74 }
75 ]
76 },
77 "isSuccessful" : true
78 }
79 ]
80}詳細は、「Composite Graph」を参照してください。