SQL Server / Zoho CRM REST APIs / Send GDPR-related data
        
        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.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.
    -- {
    --   "data": [
    --     {
    --       "from": {
    --         "user_name": "Patricia Boyle",
    --         "email": "p.boyle@abc.com"
    --       },
    --       "to": [
    --         {
    --           "user_name": "user1",
    --           "email": "user1email@zoho.com"
    --         }
    --       ],
    --       "subject": "Mail subject",
    --       "content": "<br><a href=\"${ConsentForm.en_US}\" id=\"ConsentForm\" class=\"en_US\" target=\"_blank\">Consent form link</a><br><br><br><br><br><h3><span style=\"background-color: rgb(254, 255, 102)\">REGARDS,</span></h3><div><span style=\"background-color: rgb(254, 255, 102)\">AZ</span></div><div><span style=\"background-color: rgb(254, 255, 102)\">ADMIN</span></div> <div></div>",
    --       "consent_email": true,
    --       "mail_format": "html",
    --       "data_subject_request": {
    --         "id": "{{data_subject_request_id}}",
    --         "type": "access"
    --       }
    --     }
    --   ]
    -- }
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].from.user_name', 'Patricia Boyle'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].from.email', 'p.boyle@abc.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].to[0].user_name', 'user1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].to[0].email', 'user1email@zoho.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].subject', 'Mail subject'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].content', '<br><a href="${ConsentForm.en_US}" id="ConsentForm" class="en_US" target="_blank">Consent form link</a><br><br><br><br><br><h3><span style="background-color: rgb(254, 255, 102)">REGARDS,</span></h3><div><span style="background-color: rgb(254, 255, 102)">AZ</span></div><div><span style="background-color: rgb(254, 255, 102)">ADMIN</span></div> <div></div>'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'data[0].consent_email', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].mail_format', 'html'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].data_subject_request.id', '{{data_subject_request_id}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'data[0].data_subject_request.type', 'access'
    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://domain.com/crm/v2.1/Leads/{{record_id}}/actions/send_mail', @json, 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @resp
END
GO
        Curl Command
        curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "data": [
        {
            "from": {
                "user_name": "Patricia Boyle",
                "email": "p.boyle@abc.com"
            },
            "to": [
                {
                    "user_name": "user1",
                    "email": "user1email@zoho.com"
                }
            ],
            "subject": "Mail subject",
            "content": "<br><a href=\"${ConsentForm.en_US}\" id=\"ConsentForm\" class=\"en_US\" target=\"_blank\">Consent form link</a><br><br><br><br><br><h3><span style=\"background-color: rgb(254, 255, 102)\">REGARDS,</span></h3><div><span style=\"background-color: rgb(254, 255, 102)\">AZ</span></div><div><span style=\"background-color: rgb(254, 255, 102)\">ADMIN</span></div> <div></div>",
            "consent_email": true,
            "mail_format": "html",
            "data_subject_request": {
                "id": "{{data_subject_request_id}}",
                "type": "access"
            }
        }
    ]
}'
https://domain.com/crm/v2.1/Leads/{{record_id}}/actions/send_mail
        Postman Collection Item JSON
        {
  "name": "Send GDPR-related data",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"data\": [\n        {\n            \"from\": {\n                \"user_name\": \"Patricia Boyle\",\n                \"email\": \"p.boyle@abc.com\"\n            },\n            \"to\": [\n                {\n                    \"user_name\": \"user1\",\n                    \"email\": \"user1email@zoho.com\"\n                }\n            ],\n            \"subject\": \"Mail subject\",\n            \"content\": \"<br><a href=\\\"${ConsentForm.en_US}\\\" id=\\\"ConsentForm\\\" class=\\\"en_US\\\" target=\\\"_blank\\\">Consent form link</a><br><br><br><br><br><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n            \"consent_email\": true,\n            \"mail_format\": \"html\",\n            \"data_subject_request\": {\n                \"id\": \"{{data_subject_request_id}}\",\n                \"type\": \"access\"\n            }\n        }\n    ]\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{api-domain}}/crm/v2.1/Leads/{{record_id}}/actions/send_mail",
      "host": [
        "{{api-domain}}"
      ],
      "path": [
        "crm",
        "v2.1",
        "Leads",
        "{{record_id}}",
        "actions",
        "send_mail"
      ]
    },
    "description": "Use the following keys when you have enabled GDPR compliance settings, and want to send an email asking for the user's consent or request data. Note that it is mandatory to send the consent form in the email's \"content\" key, when you ask for the lead's/contact's consent.\n\n"
  },
  "response": [
    {
      "name": "OAUTH_SCOPE_MISMATCH",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"data\": [\n        {\n            \"from\": {\n                \"user_name\": \"Patricia Boyle\",\n                \"email\": \"p.boyle@abc.com\"\n            },\n            \"to\": [\n                {\n                    \"user_name\": \"user1\",\n                    \"email\": \"user1email@zoho.com\"\n                }\n            ],\n            \"subject\": \"Mail subject\",\n            \"content\": \"<br><a href=\\\"${ConsentForm.en_US}\\\" id=\\\"ConsentForm\\\" class=\\\"en_US\\\" target=\\\"_blank\\\">Consent form link</a><br><br><br><br><br><h3><span style=\\\"background-color: rgb(254, 255, 102)\\\">REGARDS,</span></h3><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">AZ</span></div><div><span style=\\\"background-color: rgb(254, 255, 102)\\\">ADMIN</span></div> <div></div>\",\n            \"consent_email\": true,\n            \"mail_format\": \"html\",\n            \"data_subject_request\": {\n                \"id\": \"{{data_subject_request_id}}\",\n                \"type\": \"access\"\n            }\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{api-domain}}/crm/v2.1/Leads/{{record_id}}/actions/send_mail",
          "host": [
            "{{api-domain}}"
          ],
          "path": [
            "crm",
            "v2.1",
            "Leads",
            "{{record_id}}",
            "actions",
            "send_mail"
          ]
        }
      },
      "status": "Unauthorized",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "ZGS"
        },
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 08:21:44 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "113"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Expires",
          "value": "Thu, 01 Jan 1970 00:00:00 GMT"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"OAUTH_SCOPE_MISMATCH\",\n    \"details\": {},\n    \"message\": \"invalid oauth scope to access this URL\",\n    \"status\": \"error\"\n}"
    }
  ]
}