PureBasic 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)"
}'

PureBasic Example

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    ;  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    CkRest::ckAddHeader(rest,"Content-Type","application/json; charset=utf-8")
    CkRest::ckAddHeader(rest,"OData-Version","4.0")
    CkRest::ckAddHeader(rest,"Accept","application/json")
    CkRest::ckAddHeader(rest,"OData-MaxVersion","4.0")
    CkRest::ckAddHeader(rest,"Authorization","Bearer DYNAMICS_CRM_ACCESS_TOKEN")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/api/data/v9.0/contacts",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    If respStatusCode >= 400
        Debug "Response Status Code = " + Str(respStatusCode)
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf



    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)


    ProcedureReturn
EndProcedure