2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Salesforce REST API の複合(Composite)リソースで注文管理へ注文を作成してみる

Posted at

※ これから記載する事項は、私が所属する会社とは一切関係のない事柄です。

今回は Salesforce REST API の複合リソースを利用して注文管理のための注文を作成して行きたいと思います。詳細は「注文管理へのデータのインポート」をご覧ください。

Salesforce REST API の複合リソースとは?

一度に複数の要求を行うことでクライアントとサーバ間の往復回数を最小限に抑え、アプリケーションのパフォーマンスを高めます。複合リソースには下記の4つのタイプ(Composite、Batch、SObject Tree、SObject collections)がありますが、詳しくは SevenstarsRock さんの「Salesforce REST API 複合リソースを使えるようになろう!」や公式ヘルプをご確認ください。

注文管理の事前知識

注文を作成してみる

今回は SObject Tree を利用して必要最低限の情報量で注文のみとユーザ情報も含んだ注文の2つを作成していきたいと思います。SObject Tree では1回の要求で最大200件のレコードを作成でき、さらに5階層下の子オブジェクトまで作成できます。(ヘルプ

前提

  • 注文作成のために必要となるオブジェクトはヘルプ内の「注文管理でのインポート時のデータ要件」を参照してください
  • 下記のオブジェクトはすでに作成されている状態であるとします
    • 販売チャネル(SalesChannel)
    • 商品(Product2)
    • 注文配送方法(OrderDeliveryMethod)
    • (注文のみの場合)取引先(Account)の作成
  • 複合リソース API を呼ぶ際にアクセストークンが必要ですが、取得方法については「OAuth 2.0 ユーザ名パスワードフロー」であれば「Salesforce Bulk API 2.0 を使ってみる」と同様なので参考にしてみてください。

注文のみの作成

SObject Tree ではリクエストする際にルート(一番親となる)となるオブジェクトを指定する必要がありますが、顧客(取引先=Account)を作成しない場合はルートは注文(Order)として登録します。
下記のようなリクエストで注文をルートとしたオブジェクト群を作成します。(ヘルプ

リクエスト
POST /services/data/v57.0/composite/tree/Order HTTP/1.1
Host: XXXXXXX.my.salesforce.com
Content-Type: application/json
Authorization: Bearer {アクセストークン}

今回は注文作成のためのリクエストボディは下記のようにしました。(リクエストボディに関するヘルプ

リクエストボディ
{
    "records": [
        {
            "attributes": {
                "type": "Order",
                "referenceId": "refOrder1"
            },
            "AccountId": "0015h00001NF0j6AAD",
            "EffectiveDate": "2023-01-01",
            "OrderedDate": "2023-01-01",
            "SalesChannelId": "0bI5h000000TWD8EAO",
            "Status": "Draft",
            "OrderDeliveryGroups": {
                "records": [
                    {
                        "attributes": {
                            "type": "OrderDeliveryGroup",
                            "referenceId": "refOrderDeliveryGroup1"
                        },
                        "DeliverToCity": "東京都",
                        "DeliverToCountry": "日本",
                        "DeliverToName": "株式会社テスト",
                        "DeliverToPostalCode": "1000001",
                        "DeliverToState": "千代田区",
                        "DeliverToStreet": "千代田区1−1−1",
                        "OrderDeliveryMethodId": "2Dm5h000000PKLLCA4"
                    }
                ]
            },
            "OrderItems": {
                "records": [
                    {
                        "attributes": {
                            "type": "OrderItem",
                            "referenceId": "refOrderItem1"
                        },
                        "Quantity": 3,
                        "Product2id": "01t5h000009UUhpAAG",
                        "OrderDeliveryGroupId": "@{refOrderDeliveryGroup1.id}",
                        "Type": "Order Product",
                        "UnitPrice": 600,
                        "ListPrice": 800
                    }
                ]
            }
        }
    ]
}

ユーザ情報も含んだ注文

ここでは顧客も同時に作成するので、顧客(取引先=Account)をルートとして登録します。
下記のようなリクエストで顧客をルートとしたオブジェクト群を作成します。

リクエスト
POST /services/data/v57.0/composite/tree/Account HTTP/1.1
Host: XXXXXXX.my.salesforce.com
Content-Type: application/json
Authorization: Bearer {アクセストークン}

今回は注文作成のためのリクエストボディは下記のようにしました。若干情報量を増やしてあります。

リクエストボディ
{
  "records": [
      {
          "attributes": {
              "type": "Account",
              "referenceId": "refAccount1"
          },
          "AccountNumber": "123456789",
          "BillingCity": "東京都",
          "BillingCountry": "日本",
          "BillingPostalCode": "1000001",
          "BillingState": "千代田区",
          "BillingStreet": "千代田区1−1−1",
          "Name": "株式会社テスト",
          "CardPaymentMethods": {
              "records": [
                  {
                      "attributes": {
                          "type": "CardPaymentMethod",
                          "referenceId": "refCardPaymentMethod1"
                      },
                      "CardCategory": "CreditCard",
                      "CardType": "Visa",
                      "CardHolderName": "Taro Yamada",
                      "ExpiryMonth": "01",
                      "ExpiryYear": "2025",
                      "InputCardNumber": "*********1235",
                      "ProcessingMode": "External",
                      "Status": "InActive"
                  }
              ]
          },
          "Orders": {
              "records": [
                  {
                      "attributes": {
                          "type": "Order",
                          "referenceId": "refOrder1"
                      },
                      "BillingCity": "東京都",
                      "BillingCountry": "日本",
                      "BillingPostalCode": "1000001",
                      "BillingEmailAddress": "sample@example.com",
                      "BillingState": "千代田区",
                      "BillingStreet": "千代田区1−1−1",
                      "EffectiveDate": "2023-01-01",
                      "Name": "株式会社テスト",
                      "OrderedDate": "2023-01-01",
                      "OrderReferenceNumber": "123456789",
                      "SalesChannelId": "0bI5h000000TWD8EAO",
                      "Status": "Draft",
                      "OrderDeliveryGroups": {
                          "records": [
                              {
                                  "attributes": {
                                      "type": "OrderDeliveryGroup",
                                      "referenceId": "refOrderDeliveryGroup1"
                                  },
                                  "DeliverToCity": "東京都",
                                  "DeliverToCountry": "日本",
                                  "DeliverToName": "株式会社テスト",
                                  "DeliverToPostalCode": "1000001",
                                  "DeliverToState": "千代田区",
                                  "DeliverToStreet": "千代田区1−1−1",
                                  "EmailAddress": "sample@example.com",
                                  "OrderDeliveryMethodId": "2Dm5h000000PKLLCA4"
                              }
                          ]
                      },
                      "OrderItems": {
                          "records": [
                              {
                                  "attributes": {
                                      "type": "OrderItem",
                                      "referenceId": "refOrderItem1"
                                  },
                                  "Description": "GoBar Tart Cherry Hi Protein, 2oz 6 Pack",
                                  "Quantity": 3,
                                  "Product2id": "01t5h000009UUhpAAG",
                                  "OrderDeliveryGroupId": "@{refOrderDeliveryGroup1.id}",
                                  "TotalLineAmount": 2100,
                                  "Type": "Order Product",
                                  "UnitPrice": 700,
                                  "ListPrice": 800
                              }
                          ]
                      }
                  }
              ]
          }
      }
  ]
}

作成時の注意点

  • 作成するオブジェクトに対して権限があり、さらに項目へのアクセスがある必要があります
  • オブジェクト作成時に重複管理にも注意しましょう
  • オブジェクトが作成されたことによりフローが開始される場合はフロー内でエラーが出ないように注意しましょう
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?