locked
get month of date in CRM 2011 RRS feed

  • Question

  • Hi, I have a custom entity that has a date field and a currency field. it stores some amount for every month.  <One value per date (ie month) is entered>. 

    Custom Entity called 'NEW_RATE'  has Fields - date1 and Rate

    1/1/2012  $25

    2/2/2012  $35

    3/1/2012 $45 and so on.. 

    I also have a custom field called "New_invoicedate" in Invoice.  When this date is filled in on the invoice form, the plug-in should take the corresponding Rate from the Custom Entity based on the this date. 

    here the invoice date could be any date.  So, the plug-in should search for the month - i.e month of the "new_invoicedate" should be compared with the month of the date1 field from Custom Entity A.  I am trying to figure out what operator to use in the query expression? 

    rateqry.Criteria.AddCondition("new_date1", ConditionOperator.ThisMonth,  entity.GetAttributeValue<DateTime>("new_invoiceissuedate").Month);


    Saturday, June 2, 2012 8:26 PM

Answers

  • Hi,

    You will need to do a comparison of greater or equal to the month date an also less than the same date plus one month. Let me know if you need the code for this.

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by CRM elite Thursday, June 7, 2012 2:43 PM
    Saturday, June 2, 2012 8:38 PM
    Answerer
  • So I don't have the entity created to fully test, but I believe this is what you're looking for. Basically, first, get the Month of new_invoicedate and find the value that's greater than the beginning of the month, but less than the following month (if there is a following month - if there's not, then just use the value that's greater than the beginning of the month). Is that basically it?

    QueryExpression NextMonthQuery = new QueryExpression("new_rate");
                NextMonthQuery.Criteria.AddCondition("new_date1", ConditionOperator.GreaterThan, entity["new_invoiceissuedate"]);
    NextMonthQuery.AddOrder("new_date1", OrderType.Ascending);
    EntityCollection NextMonths = ServiceInstance.RetrieveMultiple(NextMonthQuery);
    DateTime? NextMonth = null;
    if (null != NextMonths)
    {
         NextMonth = Convert.ToDateTime(NextMonths[0]);
    }
    
    DateTime NewInvoiceDate = Convert.ToDateTime(entity["new_invoiceissuedate"]);          
    NewInvoiceDate = NewInvoiceDate.AddDays(-(NewInvoiceDate.Day - 1));
    QueryExpression RateQuery= new QueryExpression();
    RateQuery.Criteria.AddCondition("new_date1", ConditionOperator.GreaterEqual, NewInvoiceDate);
    if (null != NextMonth)
    {
       RateQuery.Criteria.AddCondition("new_date1", ConditionOperator.LessEqual, NextMonth);
    }


    Cordially, W.G.Ryan - www.williamgryan.mobi

    • Marked as answer by CRM elite Thursday, June 7, 2012 2:42 PM
    Monday, June 4, 2012 10:38 PM

All replies

  • Hi,

    You will need to do a comparison of greater or equal to the month date an also less than the same date plus one month. Let me know if you need the code for this.

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by CRM elite Thursday, June 7, 2012 2:43 PM
    Saturday, June 2, 2012 8:38 PM
    Answerer
  • hi scott, yes. please provide the code .  The thing is that, I am storing date in the Custom Entity. it could be any date in a given month say "6/4/2012" that would imply that, the there is a rate entered for the month of June. Now, invoice could be issued any date from 6/1/2012 through 6/30/2012 --> for e,g, if the invoice issue date is 6/20/2012, the plug-in should take the rate that correspond to the date of 6/4/2012; say if another invoice is issued on 6/29/2012, it should take the rate from same date as 6/4/2012. 
    Saturday, June 2, 2012 9:24 PM
  • So I don't have the entity created to fully test, but I believe this is what you're looking for. Basically, first, get the Month of new_invoicedate and find the value that's greater than the beginning of the month, but less than the following month (if there is a following month - if there's not, then just use the value that's greater than the beginning of the month). Is that basically it?

    QueryExpression NextMonthQuery = new QueryExpression("new_rate");
                NextMonthQuery.Criteria.AddCondition("new_date1", ConditionOperator.GreaterThan, entity["new_invoiceissuedate"]);
    NextMonthQuery.AddOrder("new_date1", OrderType.Ascending);
    EntityCollection NextMonths = ServiceInstance.RetrieveMultiple(NextMonthQuery);
    DateTime? NextMonth = null;
    if (null != NextMonths)
    {
         NextMonth = Convert.ToDateTime(NextMonths[0]);
    }
    
    DateTime NewInvoiceDate = Convert.ToDateTime(entity["new_invoiceissuedate"]);          
    NewInvoiceDate = NewInvoiceDate.AddDays(-(NewInvoiceDate.Day - 1));
    QueryExpression RateQuery= new QueryExpression();
    RateQuery.Criteria.AddCondition("new_date1", ConditionOperator.GreaterEqual, NewInvoiceDate);
    if (null != NextMonth)
    {
       RateQuery.Criteria.AddCondition("new_date1", ConditionOperator.LessEqual, NextMonth);
    }


    Cordially, W.G.Ryan - www.williamgryan.mobi

    • Marked as answer by CRM elite Thursday, June 7, 2012 2:42 PM
    Monday, June 4, 2012 10:38 PM
  • Hi Williams, Thanks. Actually what I did was that - I first ran a check to make sure invoice issue date falls in the next month (createdon) and then as Scott pointed out, I built a query to pick a Rate based on  the first and last day of that month and it worked.  
    • Edited by CRM elite Thursday, June 7, 2012 2:40 PM
    Thursday, June 7, 2012 2:39 PM