Chilkat Online Tools

Objective-C / Salesforce Platform APIs / Create Order (One-Time)

Back to Collection Items

#import <CkoHttp.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoHttp *http = [[CkoHttp alloc] init];
BOOL success;

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "allOrNone": true,
//   "compositeRequest": [
//     {
//       "method": "POST",
//       "url": "/services/data/v{{version}}/sobjects/Order",
//       "referenceId": "refOrder",
//       "body": {
//         "AccountId": "{{defaultAccount}}",
//         "EffectiveDate": "2022-01-01",
//         "Pricebook2Id": "{{standardPricebook}}",
//         "BillToContactId": "{{defaultBillToContact}}",
//         "Status": "Draft"
//       }
//     },
//     {
//       "method": "POST",
//       "url": "/services/data/v{{version}}/sobjects/AppUsageAssignment",
//       "referenceId": "refAppTag",
//       "body": {
//         "AppUsageType": "SubscriptionManagement",
//         "RecordId": "@{refOrder.id}"
//       }
//     },
//     {
//       "method": "POST",
//       "url": "/services/data/v{{version}}/sobjects/OrderAction",
//       "referenceId": "refOrderAction",
//       "body": {
//         "OrderId": "@{refOrder.id}",
//         "Type": "New Sale"
//       }
//     },
//     {
//       "method": "POST",
//       "url": "/services/data/v{{version}}/sobjects/OrderItem",
//       "referenceId": "refOrderItem",
//       "body": {
//         "OrderId": "@{refOrder.id}",
//         "OrderActionId": "@{refOrderAction.id}",
//         "PricebookEntryId": "{{ClassTrainingOneTimePBE}}",
//         "Quantity": 2.0,
//         "UnitPrice": 25.0,
//         "NetUnitPrice": 25.0,
//         "TotalLineAmount": 50.0,
//         "TaxTreatmentId": "{{defaultTaxTreatment}}"
//       }
//     }
//   ]
// }

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateBool: @"allOrNone" value: YES];
[json UpdateString: @"compositeRequest[0].method" value: @"POST"];
[json UpdateString: @"compositeRequest[0].url" value: @"/services/data/v{{version}}/sobjects/Order"];
[json UpdateString: @"compositeRequest[0].referenceId" value: @"refOrder"];
[json UpdateString: @"compositeRequest[0].body.AccountId" value: @"{{defaultAccount}}"];
[json UpdateString: @"compositeRequest[0].body.EffectiveDate" value: @"2022-01-01"];
[json UpdateString: @"compositeRequest[0].body.Pricebook2Id" value: @"{{standardPricebook}}"];
[json UpdateString: @"compositeRequest[0].body.BillToContactId" value: @"{{defaultBillToContact}}"];
[json UpdateString: @"compositeRequest[0].body.Status" value: @"Draft"];
[json UpdateString: @"compositeRequest[1].method" value: @"POST"];
[json UpdateString: @"compositeRequest[1].url" value: @"/services/data/v{{version}}/sobjects/AppUsageAssignment"];
[json UpdateString: @"compositeRequest[1].referenceId" value: @"refAppTag"];
[json UpdateString: @"compositeRequest[1].body.AppUsageType" value: @"SubscriptionManagement"];
[json UpdateString: @"compositeRequest[1].body.RecordId" value: @"@{refOrder.id}"];
[json UpdateString: @"compositeRequest[2].method" value: @"POST"];
[json UpdateString: @"compositeRequest[2].url" value: @"/services/data/v{{version}}/sobjects/OrderAction"];
[json UpdateString: @"compositeRequest[2].referenceId" value: @"refOrderAction"];
[json UpdateString: @"compositeRequest[2].body.OrderId" value: @"@{refOrder.id}"];
[json UpdateString: @"compositeRequest[2].body.Type" value: @"New Sale"];
[json UpdateString: @"compositeRequest[3].method" value: @"POST"];
[json UpdateString: @"compositeRequest[3].url" value: @"/services/data/v{{version}}/sobjects/OrderItem"];
[json UpdateString: @"compositeRequest[3].referenceId" value: @"refOrderItem"];
[json UpdateString: @"compositeRequest[3].body.OrderId" value: @"@{refOrder.id}"];
[json UpdateString: @"compositeRequest[3].body.OrderActionId" value: @"@{refOrderAction.id}"];
[json UpdateString: @"compositeRequest[3].body.PricebookEntryId" value: @"{{ClassTrainingOneTimePBE}}"];
[json UpdateNumber: @"compositeRequest[3].body.Quantity" numericStr: @"2.0"];
[json UpdateNumber: @"compositeRequest[3].body.UnitPrice" numericStr: @"25.0"];
[json UpdateNumber: @"compositeRequest[3].body.NetUnitPrice" numericStr: @"25.0"];
[json UpdateNumber: @"compositeRequest[3].body.TotalLineAmount" numericStr: @"50.0"];
[json UpdateString: @"compositeRequest[3].body.TaxTreatmentId" value: @"{{defaultTaxTreatment}}"];

// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = @"<access_token>";

CkoHttpResponse *resp = [http PostJson3: @"https://domain.com/services/data/v{{version}}/composite" contentType: @"application/json" json: json];
if (http.LastMethodSuccess == NO) {
    NSLog(@"%@",http.LastErrorText);
    return;
}

NSLog(@"%d",[resp.StatusCode intValue]);
NSLog(@"%@",resp.BodyStr);

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "allOrNone": true,
    "compositeRequest": [
        {
            "method": "POST",
            "url": "/services/data/v{{version}}/sobjects/Order",
            "referenceId": "refOrder",
            "body": {
                "AccountId": "{{defaultAccount}}",
                "EffectiveDate": "2022-01-01",
                "Pricebook2Id": "{{standardPricebook}}",
                "BillToContactId": "{{defaultBillToContact}}",
                "Status": "Draft"
            }
        },
        {
            "method": "POST",
            "url": "/services/data/v{{version}}/sobjects/AppUsageAssignment",
            "referenceId": "refAppTag",
            "body": {
                "AppUsageType": "SubscriptionManagement",
                "RecordId": "@{refOrder.id}"
            }
        },
        {
            "method": "POST",
            "url": "/services/data/v{{version}}/sobjects/OrderAction",
            "referenceId": "refOrderAction",
            "body": {
                "OrderId": "@{refOrder.id}",
                "Type": "New Sale"
            }
        },
        {
            "method": "POST",
            "url": "/services/data/v{{version}}/sobjects/OrderItem",
            "referenceId": "refOrderItem",
            "body": {
                "OrderId": "@{refOrder.id}",
                "OrderActionId": "@{refOrderAction.id}",
                "PricebookEntryId": "{{ClassTrainingOneTimePBE}}",
                "Quantity": 2.0,
                "UnitPrice": 25.0,
                "NetUnitPrice": 25.0,
                "TotalLineAmount": 50.0,
                "TaxTreatmentId": "{{defaultTaxTreatment}}"
            }
        }
    ]
}'
https://domain.com/services/data/v{{version}}/composite

Postman Collection Item JSON

{
  "name": "Create Order (One-Time)",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"allOrNone\": true,\n    \"compositeRequest\": [\n        {\n            \"method\": \"POST\",\n            \"url\": \"/services/data/v{{version}}/sobjects/Order\",\n            \"referenceId\": \"refOrder\",\n            \"body\": {\n                \"AccountId\": \"{{defaultAccount}}\",\n                \"EffectiveDate\": \"2022-01-01\",\n                \"Pricebook2Id\": \"{{standardPricebook}}\",\n                \"BillToContactId\": \"{{defaultBillToContact}}\",\n                \"Status\": \"Draft\"\n            }\n        },\n        {\n            \"method\": \"POST\",\n            \"url\": \"/services/data/v{{version}}/sobjects/AppUsageAssignment\",\n            \"referenceId\": \"refAppTag\",\n            \"body\": {\n                \"AppUsageType\": \"SubscriptionManagement\",\n                \"RecordId\": \"@{refOrder.id}\"\n            }\n        },\n        {\n            \"method\": \"POST\",\n            \"url\": \"/services/data/v{{version}}/sobjects/OrderAction\",\n            \"referenceId\": \"refOrderAction\",\n            \"body\": {\n                \"OrderId\": \"@{refOrder.id}\",\n                \"Type\": \"New Sale\"\n            }\n        },\n        {\n            \"method\": \"POST\",\n            \"url\": \"/services/data/v{{version}}/sobjects/OrderItem\",\n            \"referenceId\": \"refOrderItem\",\n            \"body\": {\n                \"OrderId\": \"@{refOrder.id}\",\n                \"OrderActionId\": \"@{refOrderAction.id}\",\n                \"PricebookEntryId\": \"{{ClassTrainingOneTimePBE}}\",\n                \"Quantity\": 2.0,\n                \"UnitPrice\": 25.0,\n                \"NetUnitPrice\": 25.0,\n                \"TotalLineAmount\": 50.0,\n                \"TaxTreatmentId\": \"{{defaultTaxTreatment}}\"\n            }\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/composite",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "composite"
      ]
    },
    "description": "Creates a Subscription Management order with a single order item (label: Order Product). The order item has a one-time product selling model."
  },
  "response": [
  ]
}