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

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: query "on or before" last month

    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.

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: query "on or before" last month

    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans