AX 2012: Add Fields to Batch Job Dialog

Posted on Updated on

Prerequisites:

1. Create a simple batch job.

Requirement:

To add custom fields to batch job dialog to choose a date range to process only those records falling in that date range.

Project overview:

Untitled

As you can see in the picture, a data contract class MAKSalesTableContract has been added to the project to achieve the goal. We’ll be adding from date and to date fields on the batch dialog to capture the date range.

Development steps:

1. Create a new data contract class MAKSalesTableContract with the following class declaration:

[DataContractAttribute]
class MAKSalesTableContract
{
    TransDate   fromDate;
    TransDate   toDate;
}

2. Create a new method parmFromDate with the following definition:

[
    DataMemberAttribute,
    SysOperationLabelAttribute(literalStr("From Date")),
    SysOperationHelpTextAttribute(literalStr("Pick a valid begin date")),
    SysOperationDisplayOrderAttribute('1')
]
public TransDate parmFromDate(TransDate _fromDate = fromDate)
{
    fromDate = _fromDate;

    return fromDate;
}

3. Create a new method parmToDate with the following definition:

[
    DataMemberAttribute,
    SysOperationLabelAttribute(literalStr("To Date")),
    SysOperationHelpTextAttribute(literalStr("Pick a valid end date")),
    SysOperationDisplayOrderAttribute('2')
]
public TransDate parmToDate(TransDate _toDate = toDate)
{
    toDate = _toDate;

    return toDate;
}

4. Update the MAKSalesTableService.processRecords method to the following definition:

[SysEntryPointAttribute(false)]
public void processRecords(MAKSalesTableContract _contract)
{
    MAKSalesTable   makSalesTable;
    int             counter = 0;

    //Determines the runtime
    if (xSession::isCLRSession())
    {
        info('Running in a CLR session.');
    }
    else
    {
        info('Running in an interpreter session.');

        //Determines the tier
        if (isRunningOnServer())
        {
            info('Running on the AOS.');
        }
        else
        {
            info('Running on the Client.');
        }
    }

    //Actual operation performed
    while select forUpdate makSalesTable
        where makSalesTable.TransDate >= _contract.parmFromDate()
            && makSalesTable.TransDate <= _contract.parmToDate()
    {
        ttsBegin;
        makSalesTable.Processed = NoYes::Yes;
        makSalesTable.update();
        ttsCommit;

        counter++;
    }

    info(strFmt("Successfully processed %1 records.", counter));
}

This is where the magic happens. Note that method now takes an input parameter of type MAKSalesTableContract class. Then it filters the records to be processed in the while loop by adding ranges to the MAKSalesTable.TransDate field, using the values given by user on the batch dialog for From Date and To Date.

5. Compile and generate incremental IL.
6. Click on Menu Items > Action > MAKSalesTableService to run the batch job.
7. You should now be having 2 fields added to the batch dialog:

Untitled

Next:

Next in the series: Add lookup to batch job dialog.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s