Objective-C Dynamics CRM: Create an Contact

Back to Index

Creates a new contact for a specific account. In this example, the account is Fourth Coffee, where the accountid = b6a19cdd-88df-e311-b8e5-6c3be5a8b200. Success is indicated by a 204 response status code with an empty response body.

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

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/contacts \
  -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 '{
    "fullname": "Gabby Cannata",
    "firstname": "Gabby",
    "lastname": "Cannata",
    "emailaddress1": "gabby@fourthcoffee.com",
    "address1_line1": "Carrera 1c No 10-01",
    "address1_city": "Bogota",
    "address1_country": "Colombia",
    "telephone1": "408-233-1939",
    "jobtitle": "Purchasing Assistant",
    "parentcustomerid_account@odata.bind": "/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)"
}'

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/contacts
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: @"fullname" value: @"Gabby Cannata"];
[json UpdateString: @"firstname" value: @"Gabby"];
[json UpdateString: @"lastname" value: @"Cannata"];
[json UpdateString: @"emailaddress1" value: @"gabby@fourthcoffee.com"];
[json UpdateString: @"address1_line1" value: @"Carrera 1c No 10-01"];
[json UpdateString: @"address1_city" value: @"Bogota"];
[json UpdateString: @"address1_country" value: @"Colombia"];
[json UpdateString: @"telephone1" value: @"408-233-1939"];
[json UpdateString: @"jobtitle" value: @"Purchasing Assistant"];
[json UpdateString: @"\"parentcustomerid_account@odata.bind\"" value: @"/accounts(b6a19cdd-88df-e311-b8e5-6c3be5a8b200)"];

[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/contacts" 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;
}