Unicode C++ Dynamics CRM: Lookup Account ID using Account Name

Back to Index

Gets the accountid by name.

Documentation: http://www.odata.org/getting-started/basic-tutorial/#queryData

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -d "$select=accountid" \
  -d "$filter=name eq 'Blue Yonder Airlines'" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN"

Unicode C++ Example

#include <CkRestW.h>
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    //  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts
    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect(L"my-dynamics-domain.api.crm.dynamics.com",port,bTls,bAutoReconnect);
    if (success != true) {
        wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    rest.AddQueryParam(L"$select",L"accountid");
    rest.AddQueryParam(L"$filter",L"name eq 'Blue Yonder Airlines'");

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

    CkStringBuilderW sbResponseBody;
    success = rest.FullRequestNoBodySb(L"GET",L"/api/data/v9.0/accounts",sbResponseBody);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",rest.responseHeader());
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",sbResponseBody.getAsString());
        return;
    }

    CkJsonObjectW jsonResponse;
    jsonResponse.LoadSb(sbResponseBody);

    int i;
    int count_i;

    const wchar_t *odataContext = jsonResponse.stringOf(L"\"@odata.context\"");
    i = 0;
    count_i = jsonResponse.SizeOfArray(L"value");
    while (i < count_i) {
        jsonResponse.put_I(i);
        const wchar_t *odataEtag = jsonResponse.stringOf(L"value[i].\"@odata.etag\"");
        const wchar_t *accountid = jsonResponse.stringOf(L"value[i].accountid");
        i = i + 1;
    }
    }

Sample JSON Response Body

{
  "@odata.context": "https://mydomain.api.crm.dynamics.com/api/data/v9.0/$metadata#accounts(accountid)",
  "value": [
    {
      "@odata.etag": "W/\"1817216\"",
      "accountid": "aca19cdd-88df-e311-b8e5-6c3be5a8b200"
    }
  ]
}