Tcl Dynamics CRM: Create an Contact

Back to Index

Creates a new contact for a specific account. In this example, the account is Fourth Coffee, where the accountid = b6a19cdd-88df-e311-b8e5-6c3be5a8b200. Success is indicated by a 204 response status code with an empty response body.

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/contact?view=dynamics-ce-odata-9

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts \
  -H "Accept: application/json" \
  -H "Content-Type: application/json; charset=utf-8" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN" \
  -d '{
    "fullname": "Gabby Cannata",
    "firstname": "Gabby",
    "lastname": "Cannata",
    "emailaddress1": "gabby@fourthcoffee.com",
    "address1_line1": "Carrera 1c No 10-01",
    "address1_city": "Bogota",
    "address1_country": "Colombia",
    "telephone1": "408-233-1939",
    "jobtitle": "Purchasing Assistant",
    "parentcustomerid_account@odata.bind": "/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)"
}'

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "my-dynamics-domain.api.crm.dynamics.com" $port $bTls $bAutoReconnect]
if {[expr $success != 1]} then {
    puts "ConnectFailReason: [CkRest_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "fullname" "Gabby Cannata"
CkJsonObject_UpdateString $json "firstname" "Gabby"
CkJsonObject_UpdateString $json "lastname" "Cannata"
CkJsonObject_UpdateString $json "emailaddress1" "gabby@fourthcoffee.com"
CkJsonObject_UpdateString $json "address1_line1" "Carrera 1c No 10-01"
CkJsonObject_UpdateString $json "address1_city" "Bogota"
CkJsonObject_UpdateString $json "address1_country" "Colombia"
CkJsonObject_UpdateString $json "telephone1" "408-233-1939"
CkJsonObject_UpdateString $json "jobtitle" "Purchasing Assistant"
CkJsonObject_UpdateString $json "\"parentcustomerid_account@odata.bind\"" "/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)"

CkRest_AddHeader $rest "Content-Type" "application/json; charset=utf-8"
CkRest_AddHeader $rest "OData-Version" "4.0"
CkRest_AddHeader $rest "Accept" "application/json"
CkRest_AddHeader $rest "OData-MaxVersion" "4.0"
CkRest_AddHeader $rest "Authorization" "Bearer DYNAMICS_CRM_ACCESS_TOKEN"

set sbRequestBody [new_CkStringBuilder]

CkJsonObject_EmitSb $json $sbRequestBody
set sbResponseBody [new_CkStringBuilder]

set success [CkRest_FullRequestSb $rest "POST" "/api/data/v9.0/contacts" $sbRequestBody $sbResponseBody]
if {[expr $success != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkJsonObject $json
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    exit
}

set respStatusCode [CkRest_ResponseStatusCode $rest]
if {[expr $respStatusCode >= 400]} then {
    puts "Response Status Code = $respStatusCode"
    puts "Response Header:"
    puts [CkRest_responseHeader $rest]
    puts "Response Body:"
    puts [CkStringBuilder_getAsString $sbResponseBody]
    delete_CkRest $rest
    delete_CkJsonObject $json
    delete_CkStringBuilder $sbRequestBody
    delete_CkStringBuilder $sbResponseBody
    exit
}


delete_CkRest $rest
delete_CkJsonObject $json
delete_CkStringBuilder $sbRequestBody
delete_CkStringBuilder $sbResponseBody