Tracking progress of scheduled tasks

In order to track the progress of a routine that isn't running synchronously, we need a method to write the state periodically. A table is clearly the best choice for this, and it may seem straightforward.

To do this, we will create a unique number and store it in the contract that acts as a correlation identifier. The calling routine would create this so that it can inquire about the status of that particular task. We could use a guid, which is a type in AX. Creating a new guid is done by this code:

guid myNewGuid = newGuid();

However, we are going to store this in a table, so we can simply use the record ID.

We will create the status record, which will contain fields to store the status (enum), the progress (such as the quantity processed and the total quantity to process), and details of any errors produced by the routine.

As the routine processes the data, it will update the record located via the record ID passed through the contract.

There is a problem, however. The routine will process its data in a transaction, and therefore, the updates to our status table will also be within that transaction. The record will appear unchanged until the routine finally issues a ttscommit. If the routine throws an error, the status record updates will also be rolled back.

The solution is to update the data in a separate transaction and make the code easier to use. This should be done as a static method on the status table.

Let's say our table is called ConFMSSysOpStatus. The method will then be as follows:

static void updateStatus(RefRecId _refId,
                         ConFMSSysOpStatus _status,
                         Qty _processed,
                         Qty _total,
                         ErrorMsg _errorMsg)
{
    UserConnection    conn;
    ConFMSSysOpStatus statusRecord;

    conn = new UserConnection();
    ttsBegin;
    statusRecord = null;
    statusRecord.setConnection(conn);

    select forupdate statusRecord
        where statusRecord.RecId == _recId;
    statusRecord.Status = _status;
    statusRecord.Processed = _processed;
    statusRecord.ErrorMsg = _errorMsg;
    statusRecord.Total = _total;
    if(statusRecord.RecId == 0)
    {
        statusRecord.insert();
    }
    else
    {
        statusRecord.update();
    }
    ttsCommit;
}

With this method, we can also allow the task to be cancelled. We can add a field based on NoYesId that the routine checks each the batch routine executes, we can choose whether to commit and exit the routine or roll the transaction back with throw error("Operation cancelled by user").

An example of this, with a progress bar on a submitting form, is included within the available downloads.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45