Default Appointment Duration

Recently someone sent me a LinkedIn message! They had a question about how to change the default duration on appointments in Microsoft Dynamics 365 from 30 mins to 1 hour. I love questions so here is my answer ….

Initially I thought of doing this using a business rule but their requirement involved wanting to use JavaScript. I actually find myself using less and less code these days, so I enjoyed the challenges of creating a few lines of code! (I’m not really a coder!!)

Additionally in my answer I wanted to avoid using Xrm.Page. (as it is deprecated!)

I decided to add some code to the On Load event of the appointment entity to achieve this. You can see in my form properties shown below how I’ve added some JavaScript and associated my “OnLoad” function to the event OnLoad.

This is simple enough but the key thing here is to remember to click the “Edit” button and select the “Pass execution context as first parameter” option. (I am doing this as I want to avoid the Xrm.Page command.)

My JavaScript is shown below. Notice that I am using the form context rather than Xrm.Page.

Also notice that I am only changing the duration when the form type is create. (I only want this to apply this default for new appointments!)

Additionally, when we change the duration directly on the form the end date will automatically update. However I found that changing the duration with code did not trigger this. So I also defaulted the end date.

My JavaScript code is below;

function OnLoad(executionContext) {
  var formContext =  executionContext.getFormContext();

  var durationAttr = formContext.getAttribute('scheduleddurationminutes');
  var endAttr = formContext.getAttribute('scheduledend');
  var startAttr = formContext.getAttribute('scheduledstart');
  var isCreateForm = (formContext.ui.getFormType() == 1);

  if(isCreateForm) {
    // ** Set the duration

    // ** I also found I had to actually set the end time!
    var newEnd = new Date(startAttr.getValue());
    var hour = newEnd.getHours();

Having made these changes and published you can see that my appointment has defaulted to 1 hour duration and the end time is one hour in advance of the start time.

This is a simple piece of JavaScript but as one person asked the question I’ve assumed others might be searching for a solution to the same issue, therefore I have responded with this blog post. Enjoy.

2 thoughts on “Default Appointment Duration

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s