Tuesday, May 13, 2014

AX 2012 - Conditionally hide a form control by current record

Occasionally we will need to change the accessibility of a certain controls/fields on a form in Dynamics AX. This can either be to hide/disable or show/enable the field from the user based on the current record they are on.

This is a pretty easy mod to do. Just follow the steps below. I'm using the example of hiding the field but you can just disable/enable the field from editing if you want using the allowEdit() property.

Your situation may be more complicated but this is meant to be a simple example. 
  1. Set the target form control's 'AutoDeclaration' property to 'yes'. by default it should be false
  2. For the primary record (e.g. in the grid), override the datasource's active() override if it is not already.
  3. After the super(), put the form control name in with the visible property. If you hit the period after the form control name and nothing pops up, 1) make sure the form control is spelled correctly and 2) check step 1 again to make sure the control has its autoDec set to yes.
    1. If the conditionals for the field to show up are complex, you might have logic and the visible property just assigning true/false, yes/no, etc outright. Example: FormControl_FieldName.visible(true);
    2. If the conditional is simple, like seeing if a field is a boolean or a type of a base enum, you can put the conditional statement in the parameters Example: FormControl_FieldName.visible(DSTableName.BaseEnumField == BaseEnum::Value2);
As an important caveat to changing any control properties on a conditional basis, if appropriate, make sure that you flip the controls to the next appropriate field. Test it out by toggling back and forth between records where the property will keep triggering back and forth.

Test your logic. Its pretty common people will have a field or button default a property to true/false, not not change the field again when moving to a new record. That means that the control will not have the value you want

Enjoy!

1 comment: