In dynamics CRM, a task, phone call, meeting are termed as activities performed on an entity. Like if there is an entity “Lead”, under a certain lead there can be multiple activities like tasks, phonecalls, appointments e.t.c

Let us create a task in dynamics CRM.

First we will define a task DTO(Data transformation Object) or model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
public class TaskEntity
    {        
        public string RegardingObjectName { get; set; }
        public string RegardingObjectId { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string OwnerId { get; set; }
        public string Subject { get; set; }
        public string Description { get; set; }
        public DateTime ScheduledStart { get; set; }
        public DateTime ScheduledEnd { get; set; }
        public string Priority { get; set; }
        public double Duration { get; set; }
    }

GetCRMService object can be referenced from following post LINK TO PART 1

ReferenceObjectId and ReferenceObjectName are important, as they represents the object to which this task will be added under. Like if you are to add a task under a Lead, then type referenceobjectname as “lead” and ReferenceObjectId as Id of the lead entity object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
public Response AddTask(TaskEntity task)
        {
            try
            {
                var crmService = GetCRMService(task.Email, task.Password);
                var setupTask = new Entity("task");
 
                setupTask.Attributes["ownerid"] = new EntityReference("systemuser", new Guid(task.OwnerId));
                setupTask.Attributes["regardingobjectid"] = new EntityReference(RegardingObjectName,
                    new Guid(task.RegardingObjectId));
                setupTask.Attributes["subject"] = task.Subject;
                setupTask.Attributes["description"] = task.Description;
                setupTask.Attributes["createdon"] = DateTime.Now.ToLocalTime();
                setupTask.Attributes["scheduledstart"] = task.ScheduledStart;
                setupTask.Attributes["scheduledend"] = task.ScheduledEnd.AddHours(23).AddMinutes(59).AddSeconds(59); // Adding end time as one day ahead of current time
                setupTask.Attributes["prioritycode"] =
                    new Microsoft.Xrm.Sdk.OptionSetValue(Convert.ToInt32(task.Priority));
                setupTask.Attributes["actualdurationminutes"] = Convert.ToInt32(task.Duration);
                crmService.Create(setupTask);
                return new Response() {Message = "success"};
            }
            catch (Exception err)
            {
                return new Response() {Message = "failure"};
            }
        }