Friday 21 August 2015

Get Fiscal Year in CRM C#

Sometimes we need to Fiscal Year in CRM and want to use the Out of the box one

Here is the query using C#, I give 2 ways, using LINQ and Late Bound;

//Using LINQ

private void RetrieveOrganizationFiscalYear()
{

            _service = new XrmContext();
            XrmContext xrmContext = new XrmContext();
            var OrganizationSettings = from os in xrmContext.OrganizationSet
                                       select new Organization

                                       {
                                           FiscalCalendarStart = os.FiscalCalendarStart,
                                       };

            DateTime dtStartFiscalYear = OrganizationSettings.First().FiscalCalendarStart.Value; 
            //this is the date of start fiscal year
 }

//Using Late Bound
 private void RetrieveOrganizationFiscalYearLateBound(IOrganizationService _service)
 {
            //Retrieve organization
            Entity enOrganization = new Entity("organization");
            EntityCollection ecOrganizations = new EntityCollection();
            DateTime dtStartFiscal = new DateTime();
            QueryExpression qx = new QueryExpression();
            qx.EntityName = "organization";
            qx.ColumnSet.AllColumns = true;
            ecOrganizations = _service.RetrieveMultiple(qx);

            if (ecOrganizations.Entities.Count > 0)
            {
                enOrganization = ecOrganizations.Entities[0];
                dtStartFiscal = enOrganization.GetAttributeValue<DateTime>("fiscalcalendarstart");
                //this is the date of start fiscal year
            }
 }

You can see this post as well:
https://community.dynamics.com/crm/f/117/t/160719

Hope this helps!

Thanks.

No comments:

Post a Comment

My Name is..