Need code to filter a report to the Current Month

rkucharrkuchar Member Posts: 33
I am trying to set up code to automatically filter a report to Shipment Date = Current month. NOT the past 30 days, just whatever the current month is. Is there a way to do this using CM, Month, D1? The compiler does not recognize anything except (TODAY) ](*,)

Answers

  • kinekine Member Posts: 12,562
    -CM -> Beginning of the month
    +CM -> End of the month
    (Of course used in CALCDATE function)
    I hope that I am correct... :-) just test it, you will see...

    Or take Date table, filter it for months, with beginning ..TODAY and ending TODAY.. and first record matching that will tell you more...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • rkucharrkuchar Member Posts: 33
    kine wrote:
    -CM -> Beginning of the month
    +CM -> End of the month
    (Of course used in CALCDATE function)
    I hope that I am correct... :-) just test it, you will see...

    Or take Date table, filter it for months, with beginning ..TODAY and ending TODAY.. and first record matching that will tell you more...


    The problem I am having is when I try to use CM the compiler says:

    Microsoft Dynamics NAV Classic
    You have specified an unknown variable.

    CM

    Define the variable under 'Global C/AL symbols'.

    OK

    I need something like this: (I know this is probably not the correct format)
    SL.SETRANGE("Shipment Date", CALCDATE(-CM), TODAY);
    

    SL is sales line table.

    I need the report to filter on all items with Shipment date within current month.
  • kinekine Member Posts: 12,562
    SL.SETRANGE("Shipment Date", CALCDATE('<-CM>', TODAY), TODAY);
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • rkucharrkuchar Member Posts: 33
    kine wrote:
    SL.SETRANGE("Shipment Date", CALCDATE('<-CM>', TODAY), TODAY);
    

    Thank you so much!

    Can I trouble you for one more detail? I just realized that I do not want TODAY. I want from -CM to + CM (Beginning of Month to End of Month). I tried changing TODAY to +CM but it would not compile.

    SL.SETRANGE("Shipment Date", CALCDATE('<-CM>', '<+CM>'), '<+CM>');
    


    Microsoft Dynamics NAV Classic
    Type conversion is not possible because 1 of the operators contains an invalid type.

    Date := Text

    OK
  • TonyHTonyH Member Posts: 223
    Use just CM without the +
  • ReinhardReinhard Member Posts: 249
    monthStart := CALCDATE('<-CM>',TODAY);
    monthEnd := CALCDATE('<CM>',TODAY);
    SL.SETRANGE("Shipment Date",monthStart,monthEnd);
    

    declaring monthStart and monthEnd isn't strictly necessary, you can put the calcdate function into the SETRANGE like in your example, I just did it like this for clarity
  • DenSterDenSter Member Posts: 8,307
    Click on the Help menu and select the C/SIDE Reference Guide. On the search tab, enter CALCDATE and hit List Topics. Read about the parameters and what data types they need to be. Once you see where the date expression goes, search again for how to build a proper date expression. Then search for SETRANGE and see what you need to enter as parameters.

    Programming these things is not difficult once you are aware of how you take care of the proper data types, and once you learn how to convert them properly. The C/SIDE Reference Guide is an excellent source of syntax help.
  • ReinhardReinhard Member Posts: 249
    ^ this +1 :)

    that is the best advice Denster.
  • rkucharrkuchar Member Posts: 33
    edited 2012-05-04
    Reinhard wrote:
    ^ this +1 :)

    that is the best advice Denster.


    Yeah I know I know. :whistle:

    I have been looking at the C/AL guide and specifically CalcDate(). I also tried to find other reports which would have code to filter on the current month. I would much rather understand it than just get an answer - but since we are in a time crunch I was hoping for some help on here.

    in Help we have:


    NewDate := CALCDATE(DateExpression [, Date])

    So

    DateExpression = '<CM>'
    Date = a "reference date." TODAY

    And now I know this works: CALCDATE('<-CM>', TODAY)

    Again from HELP

    Record.SETRANGE(Field [,FromValue] [,ToValue])

    So...

    SL.SETRANGE("Shipment Date", CALCDATE('<-CM>', TODAY), CALCDATE('<+CM>', TODAY)); [-o<

    It compiles! \:D/

    Edited to add: And it works too! :D
  • DenSterDenSter Member Posts: 8,307
    I think someone just figured out how to catch a fish :mrgreen: good to see you got that to work man =D>
Sign In or Register to comment.