Transformation Tool under the Covers: Enable on Controls

Transformation Tool under the Covers: Enable on Controls

  • Comments 0

When coding for Forms it is a common pattern to enable (or disable) controls based on values elsewhere on the Form. For example, a design may require that the ‘SEND' button is disabled until all fields are filled in and contain valid data.

In this post, I examine a simple Form and the Page output as generated from the Form Transformation Tool. To illustrate, I create a simple Form based on the Languages Table (Table 8) with no tabs and two fields, Code and Name. I've added a command button and the form looks like this when the language name is not equal to English:

stuart1

And like this when the language is English:

stuart2

 

To achieve the behaviour, I add the following code to the form:

Form – OnAfterGetRecord() Trigger

IF (Rec.Name = 'English') THEN BEGIN

  CurrForm.CommandBtn.ENABLED(TRUE)

END ELSE

  CurrForm.CommandBtn.ENABLED(FALSE)

When I run this form, I can iterate through the records and when I hit English, the Command button becomes enabled.

After using the Form Transformation Tool generating a Page, I compile and run the page. The output looks like the Page below and the functionality works. Here is a screenshot where the CommandBtn control is not enabled.

stuart3

Here is a screenshot where the CommandBtn control is enabled.

stuart4

It’s a bit more subtle than the Form but you can see the button is greyed out when not enabled.

On the Page the C/AL code has changed to:

Page – OnInit() Trigger

CommandBtnEnable := TRUE;

Page – OnAfterGetRecord() Trigger

IF (Rec.Name = 'English') THEN BEGIN

  CommandBtnEnable := FALSE ;

  MESSAGE('English');

END ELSE

  CommandBtnEnable := TRUE;

Under the covers, the CommandBtn.Editable has been mapped from a property that can be set in code to a global variable named CommandBtnEnable. This global variable is included in the data sent from the NAV Server to the RTC – this is visible from the property IncludeInDataset.

stuart5

The Form transformation tool created the variable with IncludeInDataset, created the initializing code in the Page OnInit() trigger and assigned the global variable to the Enabled property in the CommandBtn action.

Rather than setting the property directly on the control, the RoleTailored Client exposes this functionality through a global variable and binds the property value to it. The advantage of this is that even simple properties needn’t be restricted to Yes/No values but can instead be evaluated in a more complex expression from C/AL. In the case above, we use TRUE and FALSE but it could also have been an expression with multiple components that evaluate to the TRUE or FALSE logic. This expression can be shared amongst controls so you need only set a single variable to control any number of controls on a page.

Note that this article describes the behavior of the enabled property and Command Buttons. Although in the C/SIDE Classic Client similar behaviour may be observed with Menu Buttons, during transformation Menu Buttons are transformed to the Action Menu which does not support the Enabled property.

- Stuart Glasson

Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post