Visual FoxPro Dynamics CRM: Get an Account's Contacts

Back to Index

Returns the full name, email address, and phone number for each of an account's contacts. This example gets the contacts for the Coho Winery account (accountid = b0a19cdd-88df-e311-b8e5-6c3be5a8b200).

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

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts \
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -d "$select=fullname,emailaddress1,telephone1" \
  -d "$filter=_parentcustomerid_value eq b0a19cdd-88df-e311-b8e5-6c3be5a8b200" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loSbResponseBody
LOCAL lnRespStatusCode
LOCAL loJsonResponse
LOCAL i
LOCAL lnCount_i
LOCAL lcOdataContext
LOCAL lcOdataEtag
LOCAL lcFullname
LOCAL lcEmailaddress1
LOCAL lcTelephone1
LOCAL lcContactid

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("my-dynamics-domain.api.crm.dynamics.com",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? "ConnectFailReason: " + STR(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

loRest.AddQueryParam("$select","fullname,emailaddress1,telephone1")
loRest.AddQueryParam("$filter","_parentcustomerid_value eq b0a19cdd-88df-e311-b8e5-6c3be5a8b200")

loRest.AddHeader("OData-MaxVersion","4.0")
loRest.AddHeader("Accept","application/json")
loRest.AddHeader("OData-Version","4.0")
loRest.AddHeader("Authorization","Bearer DYNAMICS_CRM_ACCESS_TOKEN")

loSbResponseBody = CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess = loRest.FullRequestNoBodySb("GET","/api/data/v9.0/contacts",loSbResponseBody)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loSbResponseBody
    CANCEL
ENDIF

lnRespStatusCode = loRest.ResponseStatusCode
IF (lnRespStatusCode >= 400) THEN
    ? "Response Status Code = " + STR(lnRespStatusCode)
    ? "Response Header:"
    ? loRest.ResponseHeader
    ? "Response Body:"
    ? loSbResponseBody.GetAsString()
    RELEASE loRest
    RELEASE loSbResponseBody
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonObject')
loJsonResponse.LoadSb(loSbResponseBody)

lcOdataContext = loJsonResponse.StringOf('"@odata.context"')
i = 0
lnCount_i = loJsonResponse.SizeOfArray("value")
DO WHILE i < lnCount_i
    loJsonResponse.I = i
    lcOdataEtag = loJsonResponse.StringOf('value[i]."@odata.etag"')
    lcFullname = loJsonResponse.StringOf("value[i].fullname")
    lcEmailaddress1 = loJsonResponse.StringOf("value[i].emailaddress1")
    lcTelephone1 = loJsonResponse.StringOf("value[i].telephone1")
    lcContactid = loJsonResponse.StringOf("value[i].contactid")
    i = i + 1
ENDDO

RELEASE loRest
RELEASE loSbResponseBody
RELEASE loJsonResponse

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v9.0/$metadata#contacts(fullname,emailaddress1,telephone1)",
  "value": [
    {
      "@odata.etag": "W/\"1162014\"",
      "fullname": "Cat Francis",
      "emailaddress1": "Cat@cohowinery.com",
      "telephone1": "123-879-9879",
      "contactid": "51a0e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162210\"",
      "fullname": "Tomasz Bochenek",
      "emailaddress1": "tom@cohowinery.com",
      "telephone1": "456-698-4581",
      "contactid": "1fa1e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162593\"",
      "fullname": "Kari Furse",
      "emailaddress1": "kari@cohowinery.com",
      "telephone1": "178-854-4576",
      "contactid": "9ba2e5b9-88df-e311-b8e5-6c3be5a8b200"
    },
    {
      "@odata.etag": "W/\"1162714\"",
      "fullname": "Wilson Pais",
      "emailaddress1": "wilson@cohowinery.com",
      "telephone1": "456-698-4582",
      "contactid": "6fa5e5b9-88df-e311-b8e5-6c3be5a8b200"
    }
  ]
}