Delphi DLL Dynamics CRM: Find Tasks for an Incident

Back to Index

Returns tasks for a specific incident.

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

CURL Command

curl -X GET https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/tasks \
  -H "Accept: application/json" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -d "$select=subject,description,actualstart,activityid,_regardingobjectid_value" \
  -d "$filter=regardingobjectid_incident/incidentid eq c49e62a8-90df-e311-9565-a45d36fc5fe8" \
  -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;
subject: PWideChar;
description: PWideChar;
actualstart: PWideChar;
activityid: PWideChar;
v_regardingobjectid_value: PWideChar;

begin
rest := CkRest_Create();

//  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/tasks
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','subject,description,actualstart,activityid,_regardingobjectid_value');
CkRest_AddQueryParam(rest,'$filter','regardingobjectid_incident/incidentid eq c49e62a8-90df-e311-9565-a45d36fc5fe8');

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/tasks',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"');
    subject := CkJsonObject__stringOf(jsonResponse,'value[i].subject');
    description := CkJsonObject__stringOf(jsonResponse,'value[i].description');
    actualstart := CkJsonObject__stringOf(jsonResponse,'value[i].actualstart');
    activityid := CkJsonObject__stringOf(jsonResponse,'value[i].activityid');
    v_regardingobjectid_value := CkJsonObject__stringOf(jsonResponse,'value[i]._regardingobjectid_value');
    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#tasks(subject,description,actualstart,activityid,_regardingobjectid_value)",
  "value": [
    {
      "@odata.etag": "W/\"1173836\"",
      "subject": "Feedback on the product catalog",
      "description": "Defined and captured critical customer requirements for the product catalog. Reviewed the draft content with key players on the committee; recorded attendees. Generated email using the relevant template.",
      "actualstart": null,
      "activityid": "ca81ac0c-91df-e311-b8e5-6c3be5a8b200",
      "_regardingobjectid_value": "c49e62a8-90df-e311-9565-a45d36fc5fe8"
    }
  ]
}