top of page

Create a task when a record is created Dynamics 365 Plugins Lesson 3

How to create a task record, when a contact record is created.


I am assuming you are continuing on from Lesson 2.

Click here to see Lesson 2 - It will show you how to write your first plugin and register it to concatenate two fields into a 3rd and set you up for this lesson.


What you will learn

  • Create a new class file

  • Logic behind creating a record

  • Code explanation on creating a record

  • .Add method

  • Fill in a Single Line of Text

  • Fill in Multiple Lines of Text

  • Filling in an Option Set

  • Fill in a Date dynamically that is 7 days from today's date

  • Fill in a Lookup dynamically using ToEntityReference()

  • Build the Solution (Assembly)

  • Register/Update New Assembly with the Plugin Registration Tool

  • Register a New Step for the plugin

  • Test the plugin

Step 1 - Create a new class file for our new plugin

  • In the solution explorer, right click Plugins, select Add, and click Class.

Step 2 - Name your class

  • Change name to CreateTask.cs, and then press Add.

Before we start writing code, we need to understand the logic behind all plugins that create records in Dynamics.


THE LOGIC

Normally when we create a task record or any record, we fill out required fields, non-required fields and the system fills out some as well. We have to take these into consideration when creating a record with a plugin.

Required Fields for Task

  • Subject (Single Line of Text)

  • Activity Status (Option Set)

Non-Required Fields for Task

  • Description (Multiple Lines of Text)

  • Priority (Option Set)

  • Due (Date)

  • Duration (Option Set)

System filled

  • Owner (Lookup) - required as well, but filled in by the system.

  • Regarding (Lookup)

The regarding attribute can be filled by the user if they are creating a generic task, but if created from the activity section on an entity it will be filled in by the system. In our case we will fill it in with the contact that we create so that it shows up in the activity section on our contact.


Step 3 - Remove and replace existing code.

In visual studio, click Ctrl + A, press delete, copy this code, and paste it into your new class file in visual studio. I will explain the code below.


Code Explanation

Navigate to the code block/section labeled "try" on lines 30 - 49


Create the task record and a variable for it Line 33

Entity taskRecord = new Entity("task");

  • We create a variable, taskRecord, for the record being created and set the entity for that record by using the entity's logical name. "task"

  • We need this variable to fill out the associated fields easily.

.Add method

We will fill out all attributes using the .Add method. The .Add method takes two parameters, the attribute name and the attribute value. In Dynamics the attribute's name is the "field's logical name". Click Here to find a field's logical name.


Single Line of Text/ Multiple Lines of Text Fields - Lines 36-37

taskRecord.Attributes.Add("subject", "Call the contact");

taskRecord.Attributes.Add("description", "Ask contact for additional information");

Date Field - Line 40

taskRecord.Attributes.Add("scheduledend", DateTime.Now.AddDays(7));

  • We fill out the Due (Date) attribute using the same method as above, but set it equal to 7 day's from today's date by using DateTime.Now.AddDays(7)

  • Note that scheduledend is the logical name for the Due (Date) attribute.

Option Sets - Line 43

taskRecord.Attributes.Add("prioritycode", new OptionSetValue(1));

  • The Priority Option Set can be set to Low, Normal and High. However, Option Set attributes can not be filled in with the desired text values above. They must be filled in with the numerical value assigned to them. Low - 0, Normal - 1, High - 2

  • Click Here to find an option set's value. This is very useful for other applications and understanding system architecture.

Parent Record (Regarding) - Line 46

taskRecord.Attributes.Add("regardingobjectid", entity.ToEntityReference());

  • The regarding attribute's value needs to be filled in with the parent contact record. entity.ToEntityReference() will get the reference to the contact entity.

  • The other option is to use new EntityReference("contact", contact.Id) ,I would not use this option for this application, because it requires updating if you were to use for another entity.

Step 4 - Build the Solution (Assembly)

  • Click the save icon.

  • Select Build from the menu and then click Build Solution.

  • You should see Build Succeeded written on a blue ribbon at the bottom of the window.

Step 5 - Identify dll file location

  • In the Solution Explorer, right click on the Project/Assembly called "Plugins".

  • Select "Open Folder in File Explorer" at the bottom of the list.

  • Double click on the bin folder.

  • Double click the Debug folder.

  • You should see a file called Plugins.dll, this is your completed plugin assembly that contains two plugins, the plugin created in Lesson 2 and the one you just finished.

  • This file will be used to update our existing assembly using the plugin registration tool.

  • Exit the File Explorer

Step 6 - Update/Register New Assembly

  • Because this is a continuation from Lesson 2, we already have the assembly registered. We only need to update the assembly with the plugin registration tool. Click Here to see how to register a new assembly (step 18)

  • Open the plugin registration tool. Click here(step 16) to see how.

  • From the list of Registered Plugins & Custom Workflow Activities, click on the Assembly called Plugins.

  • Select Update.

  • In the Update Assembly: Plugins window, click on the three dots on the right. See picture below

  • Click on your dll file that you located in step 5 and click Open.

  • Your assembly should load after pressing open, but click Load Assembly to be sure.

  • After completing the steps above, The Step 2 section should show your new plugin with the one from Lesson 2 like the picture below. Check the box next to (Assembly) Plugins to let the registration tool know that you would like to update this assembly with the file you just found.

  • Click Update Selected Plugins at the bottom of the window.

  • A window will appear when it has completed, click OK.

Step 7 - Register New Step

  • Right click on the new plugin file and select Register New Step.

  • A Register New Step window will appear.

  • In the Message box type "Create"

  • In the Primary Entity box type "Contact"

  • Further down on the bottom left, change the Event Pipeline Stage of Execution to "PostOperation". We do this because we want to create the task record after the contact record has been created. I highly suggest you read my post about Event Pipeline Stages of Execution. It discusses the times in which a plugin can and should run depending on its purpose.

  • Click Register New Step

Step 8 - Test the Plugin

  • Open Dynamics and create a new contact. The plugin will run once you click save.

  • If you do not see a new task in Activities, do not worry, just refresh your browser.

  • Click on Activities and you should see your new task.

  • You will notice that the plugin from Lesson 2 is running and editing the Description Attribute.


0 comments
bottom of page