Chilkat Online Tools

SQL Server / ORACLE Hospitality OPERA Cloud REST API Workflows / Create New Profile If one doesn't exist

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.

    -- {
    --   "profileDetails": {
    --     "customer": {
    --       "personName": [
    --         {
    --           "givenName": "James",
    --           "surname": "Smith",
    --           "nameType": "Primary"
    --         },
    --         {
    --           "nameType": "Alternate"
    --         },
    --         {
    --           "nameType": "Incognito"
    --         }
    --       ],
    --       "alienInfo": {},
    --       "birthCountry": {
    --         "code": ""
    --       },
    --       "language": "E"
    --     },
    --     "addresses": {
    --       "addressInfo": [
    --         {
    --           "address": {
    --             "isValidated": false,
    --             "addressLine": [
    --               "3450 North Triumph Boulevard",
    --               "Suite 300",
    --               "",
    --               ""
    --             ],
    --             "cityName": "Lehi",
    --             "postalCode": "84043",
    --             "state": "UT",
    --             "country": {
    --               "value": "US"
    --             },
    --             "language": "E",
    --             "type": "BUSINESS",
    --             "primaryInd": true
    --           }
    --         }
    --       ]
    --     },
    --     "telephones": {
    --       "telephoneInfo": [
    --         {
    --           "telephone": {
    --             "phoneTechType": "PHONE",
    --             "phoneUseType": "HOME",
    --             "phoneNumber": "+1 385 555 0102",
    --             "orderSequence": "1",
    --             "primaryInd": true
    --           }
    --         }
    --       ]
    --     },
    --     "mailingActions": {
    --       "active": true
    --     },
    --     "privacyInfo": {
    --       "marketResearchParticipation": true,
    --       "infoFromThirdParty": true,
    --       "autoEnrollLoyaltyProgram": false,
    --       "allowPhone": true,
    --       "allowSMS": true,
    --       "allowEmail": true,
    --       "optInMailingList": true,
    --       "optInMarketResearch": true,
    --       "optInThirdParty": true,
    --       "optInAutoEnrollmentMember": true,
    --       "optInPhone": true,
    --       "optInSms": true,
    --       "optInEmail": true
    --     },
    --     "taxInfo": {},
    --     "statusCode": "Active",
    --     "requestForHotel": "{{HotelId}}",
    --     "markAsRecentlyAccessed": true,
    --     "profileType": "Guest"
    --   },
    --   "profileIdList": [
    --   ]
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.personName[0].givenName', 'James'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.personName[0].surname', 'Smith'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.personName[0].nameType', 'Primary'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.personName[1].nameType', 'Alternate'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.personName[2].nameType', 'Incognito'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'profileDetails.customer.alienInfo'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.birthCountry.code', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.customer.language', 'E'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.addresses.addressInfo[0].address.isValidated', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.addressLine[0]', '3450 North Triumph Boulevard'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.addressLine[1]', 'Suite 300'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.addressLine[2]', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.addressLine[3]', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.cityName', 'Lehi'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.postalCode', '84043'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.state', 'UT'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.country.value', 'US'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.language', 'E'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.addresses.addressInfo[0].address.type', 'BUSINESS'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.addresses.addressInfo[0].address.primaryInd', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.telephones.telephoneInfo[0].telephone.phoneTechType', 'PHONE'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.telephones.telephoneInfo[0].telephone.phoneUseType', 'HOME'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.telephones.telephoneInfo[0].telephone.phoneNumber', '+1 385 555 0102'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.telephones.telephoneInfo[0].telephone.orderSequence', '1'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.telephones.telephoneInfo[0].telephone.primaryInd', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.mailingActions.active', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.marketResearchParticipation', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.infoFromThirdParty', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.autoEnrollLoyaltyProgram', 0
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.allowPhone', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.allowSMS', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.allowEmail', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInMailingList', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInMarketResearch', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInThirdParty', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInAutoEnrollmentMember', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInPhone', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInSms', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.privacyInfo.optInEmail', 1
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'profileDetails.taxInfo'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.statusCode', 'Active'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.requestForHotel', '{{HotelId}}'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'profileDetails.markAsRecentlyAccessed', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'profileDetails.profileType', 'Guest'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'profileIdList'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-app-key', '{{AppKey}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-hotelid', '{{HotelId}}'
    -- 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/crm/v1/profiles', '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>"
	-H "Content-Type: application/json"
	-H "x-app-key: {{AppKey}}"
	-H "x-hotelid: {{HotelId}}"
	-d '{
    "profileDetails": {
        "customer": {
            "personName": [
                {
                    "givenName": "James",
                    "surname": "Smith",
                    "nameType": "Primary"
                },
                {
                    "nameType": "Alternate"
                },
                {
                    "nameType": "Incognito"
                }
            ],
            "alienInfo": {},
            "birthCountry": {
                "code": ""
            },
            "language": "E"
        },
        "addresses": {
            "addressInfo": [
                {
                    "address": {
                        "isValidated": false,
                        "addressLine": [
                            "3450 North Triumph Boulevard",
                            "Suite 300",
                            "",
                            ""
                        ],
                        "cityName": "Lehi",
                        "postalCode": "84043",
                        "state": "UT",
                        "country": {
                            "value": "US"
                        },
                        "language": "E",
                        "type": "BUSINESS",
                        "primaryInd": true
                    }
                }
            ]
        },
        "telephones": {
            "telephoneInfo": [
                {
                    "telephone": {
                        "phoneTechType": "PHONE",
                        "phoneUseType": "HOME",
                        "phoneNumber": "+1 385 555 0102",
                        "orderSequence": "1",
                        "primaryInd": true
                    }
                }
            ]
        },
        "mailingActions": {
            "active": true
        },
        "privacyInfo": {
            "marketResearchParticipation": true,
            "infoFromThirdParty": true,
            "autoEnrollLoyaltyProgram": false,
            "allowPhone": true,
            "allowSMS": true,
            "allowEmail": true,
            "optInMailingList": true,
            "optInMarketResearch": true,
            "optInThirdParty": true,
            "optInAutoEnrollmentMember": true,
            "optInPhone": true,
            "optInSms": true,
            "optInEmail": true
        },
        "taxInfo": {},
        "statusCode": "Active",
        "requestForHotel": "{{HotelId}}",
        "markAsRecentlyAccessed": true,
        "profileType": "Guest"
    },
    "profileIdList": []
}'
https://domain.com/crm/v1/profiles

Postman Collection Item JSON

{
  "name": "Create New Profile If one doesn't exist",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "var data = pm.response.headers.get(\"Location\").split(\"/\").pop();\r",
          "postman.setEnvironmentVariable(\"ProfileId\", data);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{Token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      },
      {
        "key": "x-app-key",
        "type": "text",
        "value": "{{AppKey}}"
      },
      {
        "key": "x-hotelid",
        "type": "text",
        "value": "{{HotelId}}"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"profileDetails\": {\n        \"customer\": {\n            \"personName\": [\n                {\n                    \"givenName\": \"James\",\n                    \"surname\": \"Smith\",\n                    \"nameType\": \"Primary\"\n                },\n                {\n                    \"nameType\": \"Alternate\"\n                },\n                {\n                    \"nameType\": \"Incognito\"\n                }\n            ],\n            \"alienInfo\": {},\n            \"birthCountry\": {\n                \"code\": \"\"\n            },\n            \"language\": \"E\"\n        },\n        \"addresses\": {\n            \"addressInfo\": [\n                {\n                    \"address\": {\n                        \"isValidated\": false,\n                        \"addressLine\": [\n                            \"3450 North Triumph Boulevard\",\n                            \"Suite 300\",\n                            \"\",\n                            \"\"\n                        ],\n                        \"cityName\": \"Lehi\",\n                        \"postalCode\": \"84043\",\n                        \"state\": \"UT\",\n                        \"country\": {\n                            \"value\": \"US\"\n                        },\n                        \"language\": \"E\",\n                        \"type\": \"BUSINESS\",\n                        \"primaryInd\": true\n                    }\n                }\n            ]\n        },\n        \"telephones\": {\n            \"telephoneInfo\": [\n                {\n                    \"telephone\": {\n                        \"phoneTechType\": \"PHONE\",\n                        \"phoneUseType\": \"HOME\",\n                        \"phoneNumber\": \"+1 385 555 0102\",\n                        \"orderSequence\": \"1\",\n                        \"primaryInd\": true\n                    }\n                }\n            ]\n        },\n        \"mailingActions\": {\n            \"active\": true\n        },\n        \"privacyInfo\": {\n            \"marketResearchParticipation\": true,\n            \"infoFromThirdParty\": true,\n            \"autoEnrollLoyaltyProgram\": false,\n            \"allowPhone\": true,\n            \"allowSMS\": true,\n            \"allowEmail\": true,\n            \"optInMailingList\": true,\n            \"optInMarketResearch\": true,\n            \"optInThirdParty\": true,\n            \"optInAutoEnrollmentMember\": true,\n            \"optInPhone\": true,\n            \"optInSms\": true,\n            \"optInEmail\": true\n        },\n        \"taxInfo\": {},\n        \"statusCode\": \"Active\",\n        \"requestForHotel\": \"{{HotelId}}\",\n        \"markAsRecentlyAccessed\": true,\n        \"profileType\": \"Guest\"\n    },\n    \"profileIdList\": []\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{HostName}}/crm/v1/profiles",
      "host": [
        "{{HostName}}"
      ],
      "path": [
        "crm",
        "v1",
        "profiles"
      ]
    }
  },
  "response": [
  ]
}