Thursday, 2 June 2016

How to read Organisation Business Closure in MS CRM Using C#

How to read Organisation Business Closure in MS CRM Using C#


Some time in CRM one needs to read Organisation Business Closure in code  to know whether any organisation level holiday is defined there or not.


For Ex: In scheduler services which needs to only run on  the days business is ON.











Below is the C# code snippet that can be utilise to achieve such functionality:


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  public static Boolean ISTodayOrgHolidayInOrgBusinessClosure(DateTime datetime, IOrganizationService service)
        {

            Guid orgId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).OrganizationId;
            Entity org = service.Retrieve("organization", orgId, new Microsoft.Xrm.Sdk.Query.ColumnSet("businessclosurecalendarid"));
            QueryExpression query = new QueryExpression("calendar");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria = new FilterExpression();
            query.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.Equal, org["businessclosurecalendarid"].ToString()));
            EntityCollection OrgBusinessClosureCalendarCollection = service.RetrieveMultiple(query);
            if (OrgBusinessClosureCalendarCollection.Entities.Count> 0)
            {
                Entity BusinessClosureCalendar = OrgBusinessClosureCalendarCollection.Entities[0];
                if (BusinessClosureCalendar != null)
                {
                  foreach (Entity closure in BusinessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules").Entities)
                    {
                        DateTime intervalStart = closure.GetAttributeValue<DateTime>("effectiveintervalstart");
                        DateTime intervalEnd = closure.GetAttributeValue<DateTime>("effectiveintervalend");
                        if ((DateTime.Now >= intervalStart) && (datetime <= intervalEnd))
                        {
                            return true;   //return is holiday
                        }
                    }
                }
            }
            return false;
        }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Hope it would be helpful.

Comments are highly appreciated..!!!

Happy CRMing 


1 comment:

  1. I jumped on an opportunity to purchase a rental property over the 4th of weekend. Mr Pedro was quick to respond and since this was my first time getting a loan to buy a rental property , he was able to help me walk through the loan process. It was a great experience working with a good and kind loan lender Mr Pedro. I hope you know very well if you are looking for a loan to purchase a property or funding business purposes then Mr Pedro will be able to help you with such a process here his details WhatsApp+1 863 231 0632 . / pedroloanss@gmail.com !”

    ReplyDelete