Objective-C 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
}'

Objective-C Example

#import <CkoRest.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"my-dynamics-domain.api.crm.dynamics.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];
if (success != YES) {
    NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"name" value: @"Sample Account"];
[json UpdateBool: @"creditonhold" value: NO];
[json UpdateNumber: @"address1_latitude" numericStr: @"47.639583"];
[json UpdateString: @"description" value: @"This is the description of the sample account"];
[json UpdateNumber: @"revenue" numericStr: @"5000000"];
[json UpdateNumber: @"accountcategorycode" numericStr: @"1"];

[rest AddHeader: @"Content-Type" value: @"application/json; charset=utf-8"];
[rest AddHeader: @"OData-Version" value: @"4.0"];
[rest AddHeader: @"Accept" value: @"application/json"];
[rest AddHeader: @"OData-MaxVersion" value: @"4.0"];
[rest AddHeader: @"Authorization" value: @"Bearer DYNAMICS_CRM_ACCESS_TOKEN"];

CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/api/data/v9.0/accounts" requestBody: sbRequestBody responseBody: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];
if (respStatusCode >= 400) {
    NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",rest.ResponseHeader);
    NSLog(@"%@",@"Response Body:");
    NSLog(@"%@",[sbResponseBody GetAsString]);
    return;
}