Wednesday 1 October 2014

Dynamics CRM: Create ServiceAppointment

A service appointment represents an appointment for service. The schema name for this entity is ServiceAppointment. The service appointment entity represents a block of time on a calendar. This entity stores the availability blocks for resources and can be used to determine resource availability and scheduling.
A service appointment can be customized independently from other activities to accommodate the customer's business requirements for service delivery. A service appointment must have a corresponding service. It can be already bound with a set of resources specified by an activity party (ActivityParty) list.
Following code can be used to create a Service Activity.

                // Create the ActivityParty. Resource can be a system user and user's Calendar will be booked for the duration.
                var resource = new ActivityParty
                {
                    PartyId = new EntityReference(SystemUser.EntityLogicalName, _userId)
                };

                // Create and instance of ServiceAppointment
                var appointment = new ServiceAppointment
                {
                    Subject = "Test Service Appointment",
                    ServiceId =
                        new EntityReference(Service.EntityLogicalName, new Guid("B39F04D6-7C93-E111-9675-005056B41D39")),
                    ScheduledStart = DateTime.Now.AddDays(5),
                    ScheduledEnd = DateTime.Now.AddDays(15),
                    Location = "On Site",
                    Description = "Test Appointment created for testing.",
                    Resources = new ActivityParty[] { resource },
                    IsAllDayEvent = true,
                    IsBilled = true
                };

                var request = new CreateRequest {Target = appointment};
                var response = (CreateResponse) _service.Execute(request);

                var serviceAppointmentId = response.id;

P. S. Hayer