Dynamics 365





Monday, May 28, 2018

Calculate Case Resolution time based on business calendar SLA


Calculate Case Resolution time based on business calendar SLA

//CaseHandler caseHandler  // this caseHandler to retrieve the case related fields like case create time
// you can replace it by CaseEntity

public TimeSpan CalculateCaseresolutionDuration(CaseHandler caseHandler, string incidentId, DateTime CaseCreateTime)
        {
            TimeSpan BusinessHoursDuration;
         
            try
            {
                TimeManager TimeManager = new TimeManager(service, tracer, userId, CaseManagementConstants.Module);
                var caseEntity = caseHandler.GetCaseById(incidentId, new[]
                {
//pass case attributes which are reqired               
IncidentEntityAttributeNames.ProcessConfigurationIdFieldName,IncidentEntityAttributeNames.OwnerIdFieldName,IncidentEntityAttributeNames.Entitlement
            });

                var ConfigID = CrmTypesHelper.GetEntityReferenceId(caseEntity, IncidentEntityAttributeNames.ProcessConfigurationIdFieldName);
                var OwnerID = CrmTypesHelper.GetEntityReferenceId(caseEntity, IncidentEntityAttributeNames.OwnerIdFieldName);
                Guid OwnerGuid = new Guid(OwnerID);
                Guid ConfigGUID = new Guid(ConfigID);

                SLAConfigObject slaConfig = new SLAConfigObject(service, userId, CaseManagementConstants.Module, tracer);

                SLAObject sla = slaConfig.GetCaseProcessConfigurationSLAbyConfigId(ConfigGUID, Guid.Empty, OwnerGuid);
                           

                Guid calendarGuid = TimeManager.GetCalendarFromSLA(sla.SLAId);

                var ResolutionDate = DateTime.UtcNow;
                List<TimeSlot> timeSlots = TimeManager.GetAvailableTimeSlotsForCalendar(calendarGuid, CaseCreateTime, DateTime.UtcNow);
                double TotalSpanSeconds = 0;
                double Totalhours = 0;
                double TotalBusinesshours = 0;
                double Totaldays = 0;
                double TotalMinutes = 0;
                int TotalDaysTillDay = timeSlots.Count / 2;
                foreach (var slot in timeSlots)
                {
                    double slotSpan = 0;
                    double hours = 0;
                    if (CaseCreateTime > slot.End)
                        slotSpan = 0;
                    else if (CaseCreateTime > slot.Start && ResolutionDate < slot.End)
                    {
                        slotSpan = (ResolutionDate - CaseCreateTime).TotalSeconds;
                        hours = (ResolutionDate - CaseCreateTime).TotalHours;
                    }
                    else if (CaseCreateTime > slot.Start && CaseCreateTime < slot.End && ResolutionDate > slot.End)
                    {
                        slotSpan = (slot.End - CaseCreateTime).TotalSeconds;
                        hours = (slot.End - CaseCreateTime).TotalHours;
                    }
                    else if (ResolutionDate > slot.Start && ResolutionDate < slot.End)
                    {
                        slotSpan = (ResolutionDate - slot.Start).TotalSeconds;
                        hours = (ResolutionDate - slot.Start).TotalHours;
                    }
                    else
                    {
                        slotSpan = (slot.End - slot.Start).TotalSeconds;
                        hours = (slot.End - slot.Start).TotalHours;
                    }

                    TotalSpanSeconds += slotSpan;
                    Totalhours += hours;

                }
                TotalBusinesshours = Totalhours;

                double reminingHours = Totalhours % 8;
                Totaldays = Totalhours / 8;             
                TotalMinutes = reminingHours * 60;

                BusinessHoursDuration = new TimeSpan(Convert.ToInt32(Totaldays), Convert.ToInt32(reminingHours), Convert.ToInt32(TotalMinutes), 0);


                var resolutionDuration = string.Format("{0:%d} days, {0:%h} hours, {0:%m} minutes, {0:%s} seconds", BusinessHoursDuration);
            }

            catch (Exception e)
            {
                ExceptionHelper.LogException(e, userId, CaseManagementConstants.Module, "Error occured", service);
                tracer.AddError("Error occured in Case Management, CalculateCaseresolutionDuration:" + e.Message);
                throw;
            }
            return BusinessHoursDuration;
        }

No comments:

Post a Comment