WorkCalendarSched

When programming Dynamics AX it is very easy to add days to a date. You can take todays date and just add an integer of five to get the date five days from now. But because this is easy to do it doesn’t mean that this always is the right way to go about this date business.

If you look closer at how the dates are calculated on the sales order lines or the purchase order lines in Dynamics AX you see that a class called WorkCalendarSched is used. This is because deliveries are dependant on when the company using Dynamics AX actually can send the order. Not all companies work on weekends.

This is when WorkCalendarSched comes in handy, with this class and the class method “shedDate” you can make sure that the delivery is set to a day when the company actually is going to be able deliver.

This is a basic job that uses the class WorkCalendarSched and the method “shedDate”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
static void FO_stepCalendar(Args _args)
{
    SchedDate           schedDate;
    date                fromDate = systemDateGet(),
                        testDate;
    WorkCalendarSched   workCalendarSched;
    int                 days2step = 4;
    boolean             calendarDays = false;
    CustTable           cust = CustTable::find("4000");
    ;
 
    // Without using schedDate.
    testDate = fromDate + days2step;
 
    workCalendarSched = new WorkCalendarSched();
 
    schedDate = workCalendarSched.schedDate(
                    SchedDirection::Forward,
                    fromDate,
                    days2step,              
                    calendarDays,
                    cust.SalesCalendarId,
                    CompanyInfo::find().ShippingCalendarId);
 
    print strFmt("From date: %1", fromDate);
    print strFmt("Test date: %1", testDate);
    print strFmt("Scheddate: %1", schedDate);
    print strFmt("%1", workCalendarSched.isDateOpen(
                 CompanyInfo::find().ShippingCalendarId, 
                 testDate));
    print strFmt("%1", workCalendarSched.isDateOpen(
                 CompanyInfo::find().ShippingCalendarId, 
                 schedDate));
    pause;
}

Output:

From date: 2008-03-11
Test date: 2008-03-15
Scheddate: 2008-03-17
0
1

Notice that we stepped four days forward but ended up on a date six days from now, this is because four days from now is a Saturday and according to the Calendar used, deliveries are not performed on weekends.

The method used to decide this is the method “isDateOpen”. In the “shedDate” method, Dynamics AX checks the date against both the primary and secondary calendar. In this case this means the customers calendar and then your company’s shipping calendar. If the method returns “false”, one day is either added to or substracted from the schedDate, depending on direction chosen. This is repeated until the method returns “true”. In our case we get false in Saturday, false on Sunday but true on Monday which gives the date 2008-03-17.

To get a better understanding of how it all works, check out the code in the “shedDate” method and try it out with different combinations of dates, directions and calendars.

Last 5 posts in Development

Leave a Reply

Your email address will not be published. Required fields are marked *