There are some requirements at times when we need to book Appointment in Dynamics 365 for Sales on creation of Lead with prospective Client every other day or similar other requirement, for such scenarios scheduling Appointment manually may seem to be tedious at times, so there is always a need to automatically create it from CRM and put it as an event in Outlook Calendar so as the attendees do not forget it. This Blog will show a way which would be useful to achieve such functionality easily with small C# snippet. Make sure that the mailbox of the user in question is properly setup.
Factually, the Appointment record which is created in Dynamics 365 only gets reflected in Outlook when it is Booked and Status is Busy (Scheduled), not when created. Confusing, right? Now let us understand difference between Booked and Created from the pictures given below.
Booked
Created
So, there must be a way which would dynamically create Appointment and also reflect it on Outlook with different Schedules, subjects, attendees, etc.
For such cases, Microsoft has given us a way which can be used to create Appointment record with Booked Event from C# Plugin Code with SDK Messages introduced in Dynamics 365 V9 namely BookRequest and BookResponse Class pairs, that needs to be executed in order to Book the Appointment.
You can get details of BookRequest class on this link
Follow this link for BookResponse class
Now, let us look at the sample code by taking scenario as discussed earlier on creation of lead which would book an appointment 24 hours after lead is created
public class PostLeadCreationBookAppointment { #region BookAppointment /// <summary> /// Method to Book Appointment in Dynamics 365 /// Booked Appointment Reflected in Outlook Calender /// </summary> public void Execute(IServiceProvider serviceprovider) { // Obtain the execution context from the service provider. IPluginExecutionContext executionContext = (IPluginExecutionContext)iServiceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service factory reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)iServiceProvider.GetService(typeof(IOrganizationServiceFactory)); // Obtain the organization service reference. IOrganizationService service = serviceFactory.CreateOrganizationService(executionContext.UserId); try { if (Plugin.ValidateTargetAsEntity("lead", executionContext)) { Entity leadEntity = (Entity)executionContext.InputParameters["Target"]; if (leadEntity != null) { DateTime createdOn = leadEntity.GetAttributeValue<DateTime>("createdon"); DateTime scheduleStart = createdOn.AddHours(24); DateTime scheduleEnd = scheduleStart.AddMinutes(30); //Creating Activity Party object to take Logged In User as PartyId ActivityParty party = new ActivityParty { PartyId = new EntityReference("systemuser", executionContext.UserId) }; //Declaring Appointment Entity which needs to be booked Entity appointment = new Appointment { Subject = "Sample Appointment with Booked Status", Description = "Sample Appointment which would be Reflected as Outlook Reminder", ScheduledStart = scheduleStart, ScheduledEnd = scheduleEnd, StateCode = 3, //Updating the Appointment State as Scheduled StatusCode = 5, //Updating the Appointment Status as Busy RequiredAttendees = new ActivityParty[] { party }, Organizer = new ActivityParty[] { party } }; //Using BookRequest Class to book Appointment BookRequest book = new BookRequest { Target = appointment }; //Executing booking and retieving it as BookResponse BookResponse booked = (BookResponse)service.Execute(book); } } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } } #endregion }
After registering the Plugin on creation of Lead, the Outlook Reminder would look something like below picture in Calendar of Outlook, depending upon schedule of Appointment. In this Case, the lead is created on 30th December at 10:00 AM, corresponding Appointment is being created on 31st December from 10:00 AM to 10:30 AM.
Hope this blog helps you!!
ATM Inspection PowerApp to ease ATM inspection and report generation process.
https://www.inkeysolutions.com/microsoft-power-platform/power-app/atm-inspection
Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox
Comply your Lead, Contact, and User entities of D365 CRM with GDPR compliance using the GDPR add-on.
https://www.inkeysolutions.com/microsoft-dynamics-365/dynamicscrmaddons/gdpr
Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/view-creator
© All Rights Reserved. Inkey IT Solutions Pvt. Ltd. 2024
Hi,
I try to paste your code into my plug-in but several errors comes out, first i correct all iServiceProvider in serviceProvider as you declared before. After that i’m trying to understand what kind of type is the variable Plugin at the row 21. Can you explain me how to integrate this code properly ?
Hello,
Please give us some time to check.
Instead of the entire code on line 21, please try this piece of code.
if(pluginExecutionContext != null &&
pluginExecutionContext.InputParameters.Contains(“Target”) &&
pluginExecutionContext.InputParameters[“Target”] is Entity &&
((Entity)pluginExecutionContext.InputParameters[“Target”]).LogicalName.Equals(“entityName”)){
..
}