web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

query "on or before" last month

(0) ShareShare
ReportReport
Posted on by

Hello all,

I am looking for a method where i can query records based on a date fields that is on or before last month. meaning, if i run the query today, the 31st of July, it should pick up all records where the date filed is within or before last month, which is June.

I know this isn't straight out of the box within Dynamics, unless i am not looking in the correct areas. Is this possible with FetchXML?

Thank You in advance!

Leibish Ringel

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    I don't think straight forward way it is possible in dynamics CRM Fetch XML , however you can try to create another calculated date field and create a range and retrieve using greater than less than date condition.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    From where you are planning to run the fetch. I assume it must be some custom code. If yes, then why can't you build the date and then pass it to the fetchxml. For example, below code retrieves account created on last month and older.

    ========

    using (service = new OrganizationServiceProxy(new Uri(_organizationURI), null, _credential, null))

               {

                   var currentDate = DateTime.Now;

                   var firstDayOfCurrentMonth = new DateTime(1, currentDate.Month, currentDate.Year);

                   var lastDayOfPreviousMonth = firstDayOfCurrentMonth.AddDays(-1);

                   var fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

                                             <entity name='account'>

                                               <attribute name='name' />                                                                                      

                                               <filter type='and'>

                                                 <condition attribute='createdon' operator='on-or-before' value='{0}'  />                                              

                                               </filter>

                                             </entity>

                                           </fetch>";

                   fetch = string.Format(fetch, lastDayOfPreviousMonth.ToString("yyyy-MM-dd"));

                   var reults = service.RetrieveMultiple(new FetchExpression(fetch));

               }

    =====================

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans