Saturday, January 2, 2016

How to implement Runbase form in AX

Today I had a requirement that allow users to run Outlook synchronization process in batch rather sync it manually using out of the box functionality. Let's have a look on existing functionality and then extend it to achieve the requirement of running it in batch.

Existing functionality in AX

Home> Periodic > Synchronize [AOT form name is smmOutlookSyncrhonization]

As you can see someone has to manually sync outlook emails to AX acvitities or viceversa.

Extended functionality in AX

Let's implement this functionality in batch so users can set recurrence the Sync.

Create a new class;
class SyncOutLookEmails_RunbaseForm extends RunBaseBatch
{
    // Packed
    TransDate            testDate; // I am using this variable for own testing purpose
    #define.CurrentVersion(2)
    #define.Version1(2)
    #localmacro.CurrentList
        testDate
    #endmacro

}
public Object dialog()
{
    DialogRunbase   dialog = Dialog::newFormnameRunbase(formstr(smmOutlookSyncrhonization),this);
;
    return dialog;
}
public boolean getFromDialog()
{
    boolean ret;
    ret = super();
    return ret;

}
protected void new()
{
    super();
}
public container pack()
{
    return [#CurrentVersion,#CurrentList];
}
public boolean runsImpersonated()
{
    return true;

}
public boolean showQueryValues()
{
    return true;
}
public boolean unpack(container _packedClass)
{
    boolean     ret;
    Version     version = RunBase::getVersion(_packedClass);
    ;
    switch (version)
    {
        case #CurrentVersion:
            [version, #CurrentList] = _packedClass;
            ret = true;
            break;
        default:
            ret = false;
            break;
    }
    return ret;
}
server static SyncOutLookEmails_RunbaseForm construct()
{
    return new SyncOutLookEmails_RunbaseForm();
}
server static void main(Args args)
{
    SyncOutLookEmails_RunbaseForm   syncOutLookEmails_RunbaseForm = SyncOutLookEmails_RunbaseForm::construct();
    if (syncOutLookEmails_RunbaseForm.prompt())
        syncOutLookEmails_RunbaseForm.run(); 
}
Run this class by pressing F5, Ooopsss!!! it throws an error

A DialogStartGrp group is missing from the form smmOutlookSyncrhonization. In this group dialog controls are added.
This requires to add a group on the form and this has to have under a tab/tab page, let's do this;
How the current form looks in AOT


How it should look like to use it for batch class, created above.


  • Added new Tab
  • Added new Tab page
  • Added dialogStartGrp under tab page
  • Moved exisiting groups under tab page
  • Set caption "General" to tab page.
It requires few more methods at form level to make it working in batch

public class FormRun extends ObjectRun
{
    HcmWorker                   hcmWorker;
    OutlookUserSetup            outlookUserSetup;
    TransDate                   synchronizeFromDate;
    TransDate                   synchronizeToDate;
   
    SyncOutLookEmails_RunbaseForm   syncOutLookEmails_RunbaseForm;
}

public void init()
{
    synchronizeFromDate = systemdateget();
    synchronizeToDate = systemdateget();
   
    syncOutLookEmails_RunbaseForm = element.args().caller().runbase();

    super();

    if (!smmAxaptaOutlookMapping::isOutlookMappingSetupCompleted())
    {
        smmAxaptaOutlookMapping::createDefaultSetup();
    }

    // Find worker connected to the current logged ion
    hcmWorker = HcmWorker::find(HcmWorker::userId2Worker(curuserid()));

    if (hcmWorker)
    {
        outlookUserSetup = OutlookUserSetup::findByWorker(hcmWorker.RecId);
        // Calculate synchronization period based on the employee setup parameters
        activityFromDate.dateValue(systemdateget() - outlookUserSetup.SmmSynchronizeDaysBack);
        activityToDate.dateValue(systemdateget() + outlookUserSetup.SmmSynchronizeDaysForward);
    }
    else
    {
        // No employee is mapped to the current user. Set mapping in Employee option form.
        error("@SYS80637");
        element.close();
    }
}

void closeOk()
{
    DialogRunbase dialog = element.args().caller();
;
    dialog.updateServer();
    if (syncOutLookEmails_RunbaseForm.checkCloseDialog())
        super();
}

//AOSRunMode::Client
RunBase runBase()
{
    return syncOutLookEmails_RunbaseForm;
}
Run class again by pressing F5


Target achieved!!! You can run the form with batch


Happy Dax!ng !!!