Delphi ActiveX Dynamics CRM: Create an Account

Back to Index

Creates a new account.

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/create-entity-web-api

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts \
  -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 '{
    "name": "Sample Account",
    "creditonhold": false,
    "address1_latitude": 47.639583,
    "description": "This is the description of the sample account",
    "revenue": 5000000,
    "accountcategorycode": 1
}'

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
json: TChilkatJsonObject;
sbRequestBody: TChilkatStringBuilder;
sbResponseBody: TChilkatStringBuilder;
respStatusCode: Integer;

begin
rest := TChilkatRest.Create(Self);

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

json := TChilkatJsonObject.Create(Self);
json.UpdateString('name','Sample Account');
json.UpdateBool('creditonhold',0);
json.UpdateNumber('address1_latitude','47.639583');
json.UpdateString('description','This is the description of the sample account');
json.UpdateNumber('revenue','5000000');
json.UpdateNumber('accountcategorycode','1');

rest.AddHeader('Content-Type','application/json; charset=utf-8');
rest.AddHeader('OData-Version','4.0');
rest.AddHeader('Accept','application/json');
rest.AddHeader('OData-MaxVersion','4.0');
rest.AddHeader('Authorization','Bearer DYNAMICS_CRM_ACCESS_TOKEN');

sbRequestBody := TChilkatStringBuilder.Create(Self);
json.EmitSb(sbRequestBody.ControlInterface);
sbResponseBody := TChilkatStringBuilder.Create(Self);
success := rest.FullRequestSb('POST','/api/data/v9.0/accounts',sbRequestBody.ControlInterface,sbResponseBody.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(sbResponseBody.GetAsString());
    Exit;
  end;