Microsoft Dynamics CRM - How to Create an Activity

Dynamics CRM is not the easiest API to work with and creating an activity is a multiple step process. To create an activity, you first have to create the activity object and secondly associate it with the object in a second API call. For this example, I will be creating a phone call on an account.

1. Get the ID of the account you want to create the activity on.

2. Create the activity. I recommend you create an activity in the UI and then try to retrieve it so you can see what the correct format is. To do this call:

GET /activities?where=regardingobjectid = <id of the object with an activity on it> 

Keep in mind, Dynamics CRM has a number of different types of activities. For example they have a "phonecall" activity, these are defined as the "activitytypecode". If you were to create a phone call you call POST /phonecall via the POST /{objectName} api.

You can retrieve a list of objects by calling GET /objects?getAll=true

Sample phonecall:

{
 "attributes": {
 "subject": "had a phone call",
 "actualend": 1516473667000,
 "prioritycode": 1,
 "description": "had a phone call",
 "statecode": 1,
 "statuscode": 2,
 "leftvoicemail": false,
 "activitytypecode": "phonecall",
 "regardingobjectid": "fdda942a-03f4-e611-80ec-c4346bacaac0",
 "actualstart": 1516473667000
 }
}

3. Associate the activity with the parent object.

Call PATCH /accounts/{id}
{
 "action": "associate",
 "association": {
 "entity": "phonecall",
 "id": "<id of the phone call created in step 2>",
 "associationName": "Account_Phonecalls"
 }
}

Every object relationship will have its own associationName. To find the correct association name follow these steps in the Dynamics CRM UI:

1. Go to Settings > Customizations > Customize the System.

A popup window appears

Image_2018-01-20_at_12.05.51_PM.png

2. Navigate to Entities.

3. Find the entity that you are trying to create an activity on. In my case, account.

4. Select 1:N relationships.

Image_2018-01-20_at_12.08.41_PM.png

5. Find the correct relationship. It will always be formatted {parentObjectName}_{childObjectName}s.

Mine is Account_Phonecalls.