Tuesday, July 24, 2018

SysOperation part 3: SysOperationAutomaticUIBuilder AX2012


This is the third and final part in my series of posts about SysOperation (at least for now). You can find part 1 here and part 2 here.
I will demonstrate how to override methods on the dialog (in this case modified()), and how to read and set properties of controls.
For the sake of this demo, we will add a checkbox to the dialog that will allow us to set the enabled property of the date control.
It should look like this:

1. Modify the data contract

We will need to modify the data contract so a checkbox is displayed on the dialog that we can use to enable or disable the date control.
Add this variable to the class declaration of the KlForCustTesterDataContract class:
    NoYesId     allowModifyDate;
Then add this method to that same data contract

[DataMemberAttribute,
SysOperationLabelAttribute("Enable date control")]
public NoYesId parmAllowModifyDate(NoYesId _allowModifyDate = allowModifyDate)
{
    allowModifyDate = _allowModifyDate;
    return allowModifyDate;
}
There’s nothing new here, we already did all of this in part 1

2. Extending SysOperationAutomaticUIBuilder

We will extend the SysOperationAutomaticUIBuilder class so we can override methods and properties of controls on our dialog.
First, create a new class, KlForCustTesterUIBuilder:

class KlForCustTesterUIBuilder extends SysOperationAutomaticUIBuilder
{
}

Things we will need in this class are:
  1. a variable for the date control
  2. a variable for the new checkbox control
  3. our data contract
So we add these variables to the class declaration:


class KlForCustTesterUIBuilder extends SysOperationAutomaticUIBuilder
{
    DialogField     dialogFieldAllowModifyDate;
    DialogField     dialogFieldTransDate;

    KlForCustTesterDataContract klForCustTesterDataContract;
}

2.1 Adding the override method

We will first write the method that will override the modified of the checkbox control. Simply add a new method to the KlForCustTesterUIBuilder class:


public boolean allowModifyDateModified(FormCheckBoxControl _checkBoxControl)
{
    ;
    // set enabled or disabled based on checkbox
    dialogFieldTransDate.enabled(any2enum(dialogFieldAllowModifyDate.value()));
    // or alternatively
    // dialogFieldTransDate.enabled(_checkBoxControl.checked());
   
    return true;
}


The code above sets the enabled method on the dialogFieldTransDate object based on the value property of dialogFieldAllowModifyDate object, or alternatively, the _checkBoxControl variable.
As you may remember, we declared the DialogField variables in the class declaration. Of course we will still have to initialize these variables , and that’s what we’ll do when overriding the postBuild method.

2.2 Overwriting the postBuild method

Next, we need to override the postBuild method on our KlForCustTesterUIBuilder class. This method will be called after the dialog is created, so it is a good place to put our logic.
Override the method:


public void postBuild()
{
    ;
    super();
}
Next, let’s add some code to this method, starting with the code that retrieves the data contract object, which is pretty easy:
public void postBuild()
{
    ;
    super();

    // get datacontract
    klForCustTesterDataContract = this.dataContractObject();
}


Next, we retrieve the DialogField objects:

public void postBuild()
{
    ;
    super();

    // get datacontract
    klForCustTesterDataContract = this.dataContractObject();

    // get dialog fields
    dialogFieldTransDate        = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmTransDate));

    dialogFieldAllowModifyDate  = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmAllowModifyDate));
}


In the code above, the bindInfo() method returns an object of type SysOperationUIBindInfo. This contains information about which dialog controls the data members are bound to. By providing a reference to the parmTransDate and parmAllowModifyDate member when calling the getDialogField() method, we get the dialog control that is associated with each member.
Next, we will register the method we want to override:


public void postBuild()
{
    ;
    super();

    // get datacontract
    klForCustTesterDataContract = this.dataContractObject();

    // get dialog fields
    dialogFieldTransDate        = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmTransDate));

    dialogFieldAllowModifyDate  = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmAllowModifyDate));

    // register override methods
    dialogFieldAllowModifyDate.registerOverrideMethod(methodstr(FormCheckBox
Control, modified), methodstr(KlForCustTesterUIBuilder, allowModifyDateModified), 
this);
}


As you can see, we can use the registerOverrideMethod() method to override methods on dialog. This is a huge improvement over overriding methods on dialogs in 2009. We simply point to the method we want to override (FormCheckBoxControl.modified) and the method the needs to be executed (KlForCustTesterUIBuilder.allowModifyDateModified).
Finally, we initialize the value of the enabled property, and the complete method will look like this:

public void postBuild()
{
    ;
    super();

    // get datacontract
    klForCustTesterDataContract = this.dataContractObject();

    // get dialog fields
    dialogFieldTransDate        = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmTransDate));
    dialogFieldAllowModifyDate  = this.bindInfo().getDialogField(KlForCustTester
DataContract, methodstr(KlForCustTesterDataContract, parmAllowModifyDate));

    // register override methods
    dialogFieldAllowModifyDate.registerOverrideMethod(methodstr(FormCheckBox
Control, modified), methodstr(KlForCustTesterUIBuilder, allowModifyDateModified), 
this);

    // enable/disable transdate based on checkbox
    dialogFieldTransDate.enabled(any2enum(dialogFieldAllowModifyDate.value()));
}

3. One more attribute

Now, we’ve created the UIBuilder class, but we still have to link it to our data contract. That’s what the SysOperationContractProcessingAttribute attribute is for.
To link the UIBuilder class to the data contract, open the classDeclaration method of the KlForCustTesterDataContract class, and add the SysOperationContractProcessingAttribute:

[DataContractAttribute,
    SysOperationContractProcessingAttribute(classstr(KlForCustTesterUIBuilder))]
class KlForCustTesterDataContract
{
    Name        name;
    TransDate   transDate;
    str         packedQuery;

    NoYesId     allowModifyDate;
}

4. Testing!

But wait (you already know what I’m going to say right), first click the Generate Incremental CIL button to generate CIL.



Right click the KlForCustTesterServiceController menu item we created on day 2, and choose open. You should see this dialog:


When you check the checkbox, you should see that the date field is enabled.


No comments:

Post a Comment