Saturday 25 April 2015

AX2012 R3 : Scheduling dates using X++

Hi Friends,
When required to calculate a future/past dates using X++  based on the working times defined in the working calendar in AX, workcalendarsched class can be used to take off the pain.
The below job shows how we can use the schedDate function and get the result, for simplicity I have passed the calendar name defined in my system as a string:



TransDate       reqDate = systemdateget(), newReqDate;
workCalendarSched   workCalendarSched = new workCalendarSched();
   
newReqDate = workCalendarSched.schedDate(SchedDirection::Forward,reqDate,5,false,'STANDARD');
    info(strfmt("%1",newReqDate));

Another useful method in this class is isdateopen which can be used to check if a particular date is open in the calendar. The good thing about this class is that it uses SysGlobalObjectCache to store and retrieve the values. It improves the performance when running long and complex calculations.

Notice in the below method, if the key exists in cache then the value is returned from there itself else the cache is updated.



The below method of WorkCalendarSched class initialises the variables from global cache.

The global cache scope is stored on the combination of Class + current partition and current company as highlighted below:



This class is also a good example to understand how to use SysGlobalObjectCache.
Thanks for reading the blog. Keep sharing.

Friday 10 April 2015

Importance of SaveLast() in RunBase Framework

Hi Friends,
RunBase Framework is widely used in AX. In this post, I would like to highlight the importance of SaveLast() function. Understanding of pack/unpack pattern is must to utilize this class in the most effective way.

SaveLast() is used to save the latest state of the class. That means even after the class is unpacked we can still update the packed state of the class.  



One common scenario where it is very useful is when we need to show the values of fields from the caller record on the dialog . In such scenarios this method needs to be called before the prompt() method . This way the packed state is updated with the latest values and in the dialog() method you can access the latest values. 

This can been seen in many standard AX classes which extend RunBase class





So if you are packing some variables, which you are also initialising in the main method and want to show on the dialog, then call saveLast() method before prompt() to get the most recent values.

Thanks for reading the blog. Have a great day.