Anthony Steven
Anthony Steven

Reputation: 138

How to get business hours details programmatically in Dynamics 365

I have several SLAs in my Dynamics 365 ORG. Every SLA have different business hours attach to it.

I have a requirement that needs to fetch the details of the business hours in the SLA programmatically (call within plugin/custom workflow).

The details that I need will be:

Have tried using retrievemultiple and CRM Message like QueryScheduleRequest and ExpandCalendarRequest but so far haven't manage to get all the details.

Upvotes: 2

Views: 2676

Answers (2)

Oğuz
Oğuz

Reputation: 1

QuerySchedulerRequest returns the working hours for any resource(user, equipment....etc) considering the business closures

    // Retrieve the working hours of the current user.                                              
                QueryScheduleRequest scheduleRequest = new QueryScheduleRequest
                {
                    ResourceId = userResponse.UserId,
                    Start = DateTime.Now,
                    End = DateTime.Today.AddDays(7),
                    TimeCodes = new TimeCode[] { TimeCode.Available }
                };
                QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)service.Execute(scheduleRequest);

                // Verify if some data is returned for the availability of the current user
                if (scheduleResponse.TimeInfos.Length > 0)
                {
                    Console.WriteLine("Successfully queried the working hours of the current user.");
                }

https://learn.microsoft.com/en-us/power-apps/developer/data-platform/org-service/samples/query-working-hours-user

https://learn.microsoft.com/en-us/dotnet/api/microsoft.crm.sdk.messages.queryschedulerequest?view=dataverse-sdk-latest

Upvotes: 0

Dave Clark
Dave Clark

Reputation: 2303

You can get business hours (calendar) details manually but there are quite a few steps. I've outlined them below roughly, but I'd recommend you create a quick console application to debug and step through the attributes available to you in real time.

First query for the SLA you want. If you have the ID then use:

var sla = service.Retrieve("sla", ID, new ColumnSet(new string[] { "businesshoursid" });

Then get the ID of the business hours associated with the SLA:

var businessHoursId = sla.GetAttributeValue<EntityReference>("businesshoursid").Id;

Then retrieve the business hours (calendar) itself:

var calendar = service.Retrieve("calendar", businessHoursId, new ColumnSet(true));

A calendar can have multiple rules. Retrieve them using:

var calendarRules = calendar.GetAttributeValue<EntityCollection>("calendarrules");

These outer calendar rules will have a pattern which you can see using

var firstRulePattern = calendarRules[0].GetAttributeValue<string>("pattern");
// FREQ=WEEKLY;INTERVAL=1;BYDAY=SU,MO,TU,WE,TH,FR

But to get the working hours each day, you'll need the inner calendars. Using the first rule as an example:

var innerCalendarId = calendarRules[0].GetAttributeValue<EntityReference>("innercalendarid").Id;
var innerCalendar = service.Retrieve("calendar", innerCalendarId, new ColumnSet(true);
var innnerCalendarRule = innerCalendar.GetAttributeValue<EntityCollection>("calendarrules").Entities.FirstOrDefault();

From your inner rule, note the attributes duration and offset. These values are in minutes and will give you the working hours each day.

Upvotes: 2

Related Questions