简体   繁体   中英

Find break hours in work hours(from user entity) in crm 2013

I have set the working hours from user entity in crm like thi

8 AM to 1 PM 1 Pm to 2 PM break hours 2 PM to 5 PM

I have used following code which is making sure that the user is available or not.

QueryScheduleRequest scheduleRequest = new QueryScheduleRequest
{
    ResourceId = Guid.Parse("E086A8A2-A741-4BB8-8BFC-61829949CBD9"),
    Start = DateTime.Now.ToUniversalTime(),
    End = DateTime.Now.AddMinutes(1).ToUniversalTime(),
    TimeCodes = new TimeCode[] { TimeCode.Available }
};

QueryScheduleResponse scheduleResponse = 
                   (QueryScheduleResponse)_serviceProxy.Execute(scheduleRequest);


if (scheduleResponse.TimeInfos.Length > 0)
{
    // User is available
}

Now if i check this code between 1 PM to 2 PM still it is showing me user is available in spite of it is break time. Is there any way by which we can find that it is break hours. Above code is working like if i check out side between 8 AM to 5PM then and then It does not show User is available. I dont want to include break time

the answer is to use TimeCode.Unavailable like this:

QueryScheduleRequest scheduleRequest = new QueryScheduleRequest
                {
                    ResourceId = userResponse.UserId,
                    Start = DateTime.Now,
                    End = DateTime.Today.AddDays(7),
                    TimeCodes = new TimeCode[] { TimeCode.Unavailable }
                };
                QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)_orgService.Execute(scheduleRequest);

And u will get the Break time)

sorry if this is too late but this worked for me...

in my case i only used what i needed but this can be altered accordingly...

        WhoAmIResponse userResponse = (WhoAmIResponse)service.Execute(new WhoAmIRequest());

        QueryScheduleRequest req = new QueryScheduleRequest
        {
            ResourceId = userResponse.UserId,
            Start = DateTime.Today,
            End = DateTime.Today.AddDays(1),
            TimeCodes = new TimeCode[] { TimeCode.Available, TimeCode.Unavailable, TimeCode.Busy, TimeCode.Filter }
        };

        QueryScheduleResponse resp = service.Execute(req) as QueryScheduleResponse;
        bool free = true;
        foreach (TimeInfo timeInfo in resp.TimeInfos)
        {
            if (timeInfo.SubCode == SubCode.Break && DateTime.UtcNow > timeInfo.Start && DateTime.UtcNow < timeInfo.End)
            {
                free = false;
                Console.WriteLine("on break");
            }else if (timeInfo.SubCode == SubCode.Schedulable && DateTime.UtcNow < timeInfo.Start && DateTime.UtcNow > timeInfo.End)
            {
                free = false;
                Console.WriteLine("out of work hours");
            }
        }

        if (free)
        {
            Console.WriteLine("Free");
        }

        Console.ReadKey();

Please let me know if this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM