Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Promotions Creation

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- {
    --   "name": "Free Beverage",
    --   "startDateTime": "2023-06-30T00:00:00.000Z",
    --   "displayName": "Free Beverage",
    --   "priorityNumber": 1,
    --   "description": "Buy 2 Get 1 free",
    --   "endDateTime": "2023-12-30T00:00:00.000Z",
    --   "objective": "To increase sales",
    --   "loyaltyProgram": {
    --     "name": "Program-001"
    --   },
    --   "additionalFieldValues": {
    --     "attributes": {
    --       "promotionLocation__c": "HYD"
    --     }
    --   },
    --   "promotionLimits": {
    --     "perLineItemLimit": 10,
    --     "vouchersLimit": 100,
    --     "vouchersPerCustomerLimit": 5,
    --     "perCartLimit": 5,
    --     "usePerCustomerLimit": 5,
    --     "liabilityLimit": 10000,
    --     "loyaltyProgramCurrency": {
    --       "id": "0lcSB00000001TxYAI"
    --     },
    --     "pointsPerCustomerLimit": 5
    --   },
    --   "promotionEligibility": {
    --     "eligibleProductCategories": [
    --       {
    --         "id": "0ZGSB0000002td74AA"
    --       }
    --     ],
    --     "eligibleProducts": [
    --       {
    --         "name": "Coke"
    --       }
    --     ],
    --     "eligibleChannels": [
    --       {
    --         "channelType": "Offline",
    --         "store": {
    --           "name": "test Store"
    --         },
    --         "retailLocationGroup": {
    --           "name": "test"
    --         }
    --       }
    --     ],
    --     "eligibleCustomerEvents": {
    --       "event": "MemberBirthday",
    --       "eventPeriodUnit": "Date"
    --     },
    --     "eligibleEnrollmentPeriod": {
    --       "isEnrollmentRequired": true,
    --       "enrollmentStartDate": "2023-06-30T00:00:00.000Z",
    --       "enrollmentEndDate": "2023-12-30T00:00:00.000Z"
    --     },
    --     "eligibleLoyaltyTiers": [
    --       {
    --         "tierGroup": {
    --           "name": "Group-001",
    --           "id": "0ltSB00000000T3YAI"
    --         },
    --         "tier": {
    --           "name": "Silver",
    --           "id": "0lgSB00000000srYAA"
    --         }
    --       }
    --     ]
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Free Beverage'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'startDateTime', '2023-06-30T00:00:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'displayName', 'Free Beverage'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'priorityNumber', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'description', 'Buy 2 Get 1 free'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'endDateTime', '2023-12-30T00:00:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'objective', 'To increase sales'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'loyaltyProgram.name', 'Program-001'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'additionalFieldValues.attributes.promotionLocation__c', 'HYD'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.perLineItemLimit', 10
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.vouchersLimit', 100
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.vouchersPerCustomerLimit', 5
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.perCartLimit', 5
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.usePerCustomerLimit', 5
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.liabilityLimit', 10000
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionLimits.loyaltyProgramCurrency.id', '0lcSB00000001TxYAI'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'promotionLimits.pointsPerCustomerLimit', 5
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleProductCategories[0].id', '0ZGSB0000002td74AA'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleProducts[0].name', 'Coke'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleChannels[0].channelType', 'Offline'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleChannels[0].store.name', 'test Store'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleChannels[0].retailLocationGroup.name', 'test'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleCustomerEvents.event', 'MemberBirthday'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleCustomerEvents.eventPeriodUnit', 'Date'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'promotionEligibility.eligibleEnrollmentPeriod.isEnrollmentRequired', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleEnrollmentPeriod.enrollmentStartDate', '2023-06-30T00:00:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleEnrollmentPeriod.enrollmentEndDate', '2023-12-30T00:00:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleLoyaltyTiers[0].tierGroup.name', 'Group-001'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleLoyaltyTiers[0].tierGroup.id', '0ltSB00000000T3YAI'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleLoyaltyTiers[0].tier.name', 'Silver'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'promotionEligibility.eligibleLoyaltyTiers[0].tier.id', '0lgSB00000000srYAA'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/services/data/v{{version}}/global-promotions-management/promotions', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
   "name":"Free Beverage",
   "startDateTime":"2023-06-30T00:00:00.000Z",
   "displayName":"Free Beverage",
   "priorityNumber":1,
   "description":"Buy 2 Get 1 free",
   "endDateTime":"2023-12-30T00:00:00.000Z",
   "objective":"To increase sales",
   "loyaltyProgram":{
      "name":"Program-001"
   },
   "additionalFieldValues":{
      "attributes":{
         "promotionLocation__c":"HYD"
      }
   },
   "promotionLimits":{
      "perLineItemLimit":10,
      "vouchersLimit":100,
      "vouchersPerCustomerLimit":5,
      "perCartLimit":5,
      "usePerCustomerLimit":5,
      "liabilityLimit":10000,
      "loyaltyProgramCurrency":{
         "id":"0lcSB00000001TxYAI"
      },
      "pointsPerCustomerLimit":5
   },
   "promotionEligibility":{
      "eligibleProductCategories":[
         {
            "id":"0ZGSB0000002td74AA"
         }
      ],
      "eligibleProducts":[
         {
            "name":"Coke"
         }
      ],
      "eligibleChannels":[
         {
            "channelType":"Offline",
            "store":{
               "name":"test Store"
            },
            "retailLocationGroup":{
               "name":"test"
            }
         }
      ],
      "eligibleCustomerEvents":{
         "event":"MemberBirthday",
         "eventPeriodUnit":"Date"
      },
      "eligibleEnrollmentPeriod":{
         "isEnrollmentRequired":true,
         "enrollmentStartDate":"2023-06-30T00:00:00.000Z",
         "enrollmentEndDate":"2023-12-30T00:00:00.000Z"
      },
      "eligibleLoyaltyTiers":[
         {
            "tierGroup":{
               "name":"Group-001",
               "id":"0ltSB00000000T3YAI"
            },
            "tier":{
               "name":"Silver",
               "id":"0lgSB00000000srYAA"
            }
         }
      ]
   }
}'
https://domain.com/services/data/v{{version}}/global-promotions-management/promotions

Postman Collection Item JSON

{
  "name": "Promotions Creation",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "//pm.variables.set('loyaltyProgramName','xyz');",
          "pm.variables.set('processName','TierProcessing');"
        ],
        "type": "text/javascript",
        "packages": {}
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n   \"name\":\"Free Beverage\",\n   \"startDateTime\":\"2023-06-30T00:00:00.000Z\",\n   \"displayName\":\"Free Beverage\",\n   \"priorityNumber\":1,\n   \"description\":\"Buy 2 Get 1 free\",\n   \"endDateTime\":\"2023-12-30T00:00:00.000Z\",\n   \"objective\":\"To increase sales\",\n   \"loyaltyProgram\":{\n      \"name\":\"Program-001\"\n   },\n   \"additionalFieldValues\":{\n      \"attributes\":{\n         \"promotionLocation__c\":\"HYD\"\n      }\n   },\n   \"promotionLimits\":{\n      \"perLineItemLimit\":10,\n      \"vouchersLimit\":100,\n      \"vouchersPerCustomerLimit\":5,\n      \"perCartLimit\":5,\n      \"usePerCustomerLimit\":5,\n      \"liabilityLimit\":10000,\n      \"loyaltyProgramCurrency\":{\n         \"id\":\"0lcSB00000001TxYAI\"\n      },\n      \"pointsPerCustomerLimit\":5\n   },\n   \"promotionEligibility\":{\n      \"eligibleProductCategories\":[\n         {\n            \"id\":\"0ZGSB0000002td74AA\"\n         }\n      ],\n      \"eligibleProducts\":[\n         {\n            \"name\":\"Coke\"\n         }\n      ],\n      \"eligibleChannels\":[\n         {\n            \"channelType\":\"Offline\",\n            \"store\":{\n               \"name\":\"test Store\"\n            },\n            \"retailLocationGroup\":{\n               \"name\":\"test\"\n            }\n         }\n      ],\n      \"eligibleCustomerEvents\":{\n         \"event\":\"MemberBirthday\",\n         \"eventPeriodUnit\":\"Date\"\n      },\n      \"eligibleEnrollmentPeriod\":{\n         \"isEnrollmentRequired\":true,\n         \"enrollmentStartDate\":\"2023-06-30T00:00:00.000Z\",\n         \"enrollmentEndDate\":\"2023-12-30T00:00:00.000Z\"\n      },\n      \"eligibleLoyaltyTiers\":[\n         {\n            \"tierGroup\":{\n               \"name\":\"Group-001\",\n               \"id\":\"0ltSB00000000T3YAI\"\n            },\n            \"tier\":{\n               \"name\":\"Silver\",\n               \"id\":\"0lgSB00000000srYAA\"\n            }\n         }\n      ]\n   }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/global-promotions-management/promotions",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "global-promotions-management",
        "promotions"
      ],
      "query": [
        {
          "key": "journalTypeId",
          "value": "",
          "disabled": true
        }
      ]
    },
    "description": "[Promotions Creation](https://developer.salesforce.com/docs/atlas.en-us.loyalty.meta/loyalty/connect_resources_unified_promotions.htm)\n\nNOTE:\n\nIn the Pre-request. Script Tab, make changes in the variables value accordingly with the real Process Name to invoke."
  },
  "response": [
  ]
}