Inkey Solution Logo
banner

Blogs

Creating Appointment of Dynamics 365 for Sales with Plugin which would reflect in Outlook Calendar as a reminder.

, January 21, 2021 5536 Views

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

  • In above picture we can see Event as “Book” which ensures that Appointment will be shown in Outlook as a reminder.
  • Appointment created Manually from Appointment Main form are created with “Book” event

Created

  • In above picture we can see Event as “Create”, which wouldn’t reflect Appointment of Dynamics 365 as Outlook reminder
  • Appointment Created with Quick Create form or from Background process Like Workflow or Plugins are created with “Create” event.

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

https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.bookrequest?view=dynamics-general-ce-9

Follow this link for BookResponse class

https://docs.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.bookresponse?view=dynamics-general-ce-9

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

 

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

3 responses to “Creating Appointment of Dynamics 365 for Sales with Plugin which would reflect in Outlook Calendar as a reminder.”

  1. Antonio says:

    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 ?

    • Admin says:

      Hello,

      Please give us some time to check.

      • Admin says:

        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”)){
        ..
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?