Delphi DLL 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"

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
jsonResponse: HCkJsonObject;
i: Integer;
count_i: Integer;
odataContext: PWideChar;
odataEtag: PWideChar;
fullname: PWideChar;
emailaddress1: PWideChar;
telephone1: PWideChar;
contactid: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'my-dynamics-domain.api.crm.dynamics.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_AddQueryParam(rest,'$select','fullname,emailaddress1,telephone1');
CkRest_AddQueryParam(rest,'$filter','_parentcustomerid_value eq b0a19cdd-88df-e311-b8e5-6c3be5a8b200');

CkRest_AddHeader(rest,'OData-MaxVersion','4.0');
CkRest_AddHeader(rest,'Accept','application/json');
CkRest_AddHeader(rest,'OData-Version','4.0');
CkRest_AddHeader(rest,'Authorization','Bearer DYNAMICS_CRM_ACCESS_TOKEN');

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/api/data/v9.0/contacts',sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkRest__responseHeader(rest));
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_LoadSb(jsonResponse,sbResponseBody);

odataContext := CkJsonObject__stringOf(jsonResponse,'"@odata.context"');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'value');
while i < count_i do
  begin
CkJsonObject_putI(jsonResponse,i);
    odataEtag := CkJsonObject__stringOf(jsonResponse,'value[i]."@odata.etag"');
    fullname := CkJsonObject__stringOf(jsonResponse,'value[i].fullname');
    emailaddress1 := CkJsonObject__stringOf(jsonResponse,'value[i].emailaddress1');
    telephone1 := CkJsonObject__stringOf(jsonResponse,'value[i].telephone1');
    contactid := CkJsonObject__stringOf(jsonResponse,'value[i].contactid');
    i := i + 1;
  end;

CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jsonResponse);

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"
    }
  ]
}