Thursday, December 18, 2008

Change the combobox options in RunbaseBatch class

RunbaseBatch class supplies some features, including batch processing, pack/unpack and progress bar etc. On the other hand, we get this request frequently, usually to modify the properties of dialog control in runtime. For example, we add a combobox control on the dialog, but the options of combobox can't be predefined. Well, the combobox can be very conveniently filled with field lists if you are using a form, while the combobox's selection lists in RunbaseBatch is determined by Enum values ( dialog.addField(TypeId(someEnum) ). How can we accomplish this? 
First option, change the eunm's values dynamically? Oh, terrible, this means that Axapta needs to add, edit and save AOT node, then synchronize in run time.
Second option, use Form instead of RunbaseBatch class, if you want this Form to be savable and batchable, you need to manually to add some codes on it. 
Well, it is no point to do that if there is any other work around.  
There is a third option, and I believe this should do the trick.
Let's start with a simple RunBaseBatch class and a simple scenario, dynamically adding printers' name to the combobox. 
Define a BaseEnum called TestRunBaseDialog (ensure that the style property is set to combo box), and create an element for it which is called TestComboboxElement. Then we can set the TestComboboxElement's label as "Select a printer".
Now we can create the RunbaseBatch class from scratch. 
 
Declare the class
class TestAddComboOptionsInRunbase extends runbasebatch
{
    int                 printerName;
    DialogField dialogPrinter;
    FormComboboxControl controlCombobox;
    #define.CurrentVersion(1)
    #localmacro.CurrentList
        printerName
    #endmacro
}
 
Pack the value
public container pack()
{
    return [#CurrentVersion, #CurrentList];
}
 
Unpack the value
public boolean unpack(container packedClass)
{
    boolean         _ret;
    Integer         _version    = runbase::getVersion(packedClass);
    switch (_version)
    {
        case #CurrentVersion:
            [_version, #CurrentList] = packedClass;
            break;
        default:
            _ret = false;
    }
    return _ret;
}
 
Design the dialog, we need to activate the dialogSelectCtrl method. And please notice that, there should be some other dialog control to be added before dialogPrinter. The reason is that after the Dialog form has been built, the selected control can not be the dialogPrinter, otherwise, we can't implement the dialogSelectCtrl method when we look up the options in dialogPrinter combobox. So we add a Text before adding combobox. 
protected Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
{
    DialogRunBase dialog;
    ;
    controlCombobox = new FormComboboxControl();
    dialog = super(_dialog, _forceOnClient);
    dialog.caption("Dialog for printer option");
    dialog.addText("Choose Printer");
    dialogPrinter = dialog.addField(typeId(TestRunBaseDialog));
  
    dialog.allowUpdateOnSelectCtrl(true); 
    return dialog;
}
 
Okey, we can define how to change the selection list in the combobox now.
public void dialogSelectCtrl()
{
    controlCombobox = dialogPrinter.fieldControl();
    controlCombobox.clear();
    controlCombobox.add("Printer One");
    controlCombobox.add("Printer Two");
    controlCombobox.add("Printer three");
    //… more printers here
}
 
Process your business logic in run()
void run()
{
    ;
    switch (printerName)
    {
        case 0:
        info ("You are choosing printer one.");
        break;
        case 1:
        info ("You are choosing printer two.");
        break;
        case 2:
        info ("You are choosing printer three.");
        break;
    }
    //Put your logic here
}

Finally, put the codes to Main()
static void main(Args _args)
{
    TestAddComboOptionsInRunbase testRunBase = new TestAddComboOptionsInRunbase ();
    ;
    if (testRunBase.prompt())
    {
        testRunBase.run();
    }
}
 
The reason why we use dialogSelectCtrl() is that the dialog control in RunbaseBatch class is not built before the prompt method, the dialogPrinter is still the object of FormBuildControl. Only FormComboboxControl object can add and change the selection list. After Axapta implement the runbaseBatch.prompt(), dialogPrinter has become the object of FormComboboxControl. So in the dialogSelectCtrl(), we can modify the selection list of dialogPrinter combobox. 

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...