My 2p about ERP Solutions, Information Worker Solutions and other software products (mainly Microsoft Dynamics AX and Microsoft SharePoint).

15 November 2009

Run a batch job with as dialog field value today

by Patrik Luca 3 comments

Tag



Business requirement: run a batch job with as dialog field value today

I you want to run a batch job daily, which has a date dialog field as input, you often don’t want the value in the date dialog to remain the same (being the value entered upon creation of the batch job), but to be equal to today (it is the day on which the batch job is run). However, you cannot enter a variable value in the dialog upon adding the batch job to the batch list in Dynamics AX.

Solution: adapt the run method of your batch class

Let’s take as example a periodic job to clean up the sales update history in Dynamics AX (Accounts receivable > Periodic > Clean up > Sales update history clean up). You can enter a Created until date to specify until which date the history should be deleted. Suppose you want to run this as a daily batch job at night, which should delete all history until today. Entering a date value of today and adding the job to the batch list wouldn’t achieve what you want. Tonight, it would delete all history until today, but tomorrow, all history would be deleted until the day before, since your Created until date would still be the same in the job in your batch list.

To solve this add following code in the run method of the involved class (in our example SalesParmCleanUp)

void run() 
{

    #OCCRetryCount


    // if class is run in batch mode, set the
// createdDate dialog field equal to todays date

    if(this.isInBatch())
    {
        createdDate = SystemDateGet();
    }

    try
    {

        ttsbegin;

        this.deleteSalesParmTables();

        ttscommit;
    }


}

Comments 3 comments
Kevin said...

How can I do this same thing if I am using a custom report that is not called from a class? The report extends ObjectRun.

Kevin said...

I will answer my own question. I added this at the start of my fetch method:

SysReportRun sysReportRun;
;

sysReportRun = this;
if (sysReportRun.isBatchMode())
{
runDate = today();
}

Anonymous said...

Thank you Kevin

That helped me, but I had to do it this way:

sysReportRun.runbaseReport().isInBatch()

::Steffen

Patrik Luca, Ieper, BELGIUM
Feel free to use or spread all of the content on my blog. In return, linking back to my blog would be greatly appreciated. All my posts and articles are provided "AS IS" with no warranties.

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Subscribe feeds rss Most Read Entries

Subscribe feeds rss Recent Entries

Categories

Recommended Books


Subscribe feeds rss Recent Comments

This Blog is part of the U Comment I Follow movement in blogosphere. Means the comment field of this blog is made DOFOLLOW. Spam wont be tolerated.

Live Traffic Feed

Recent Visitors

Blog Archive

My Blog List

Followers

Guest Links