Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Disable grid field based on other field value

Posted on by Microsoft Employee

Hello:

I have a grid on a custom form with a FieldA I'd like to disable / not allow change if another FieldB on the same record is of a specific value. I tried code in the datasource's active method but it only works on the first record. I'd like to have all records loaded to on that grid to enable/disable FieldA if FieldB has a certain value. 

if (TBL.FieldB == NoYes::Yes)

TBL_ds.object(fieldNum(TBL, FieldA)).allowEdit(false);

else

TBL_ds.object(fieldNum(TBL, FieldA)).allowEdit(true);

Any help would be appreciated.

Thank you.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable grid field based on other field value

    Thank you Martin. The behavior seems a bit erratic on the form but hopefully the users can live with it.

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Disable grid field based on other field value

    Again, there is no setup for individual rows. You're setting property of the grid column, which is obviously just one, therefore it always applies to all records.

    But it's not any problem, because your logic will always execute and enable or disable the field as needed, when users switch to the record they want to edit.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable grid field based on other field value

    Hi Martin,

    Hopefully I can explain this clearly. With the code in place, once I open the form, the currently selected field would render with FieldA grayed out because FieldB for that record has a Yes value. As I click each record beneath, each would render with FieldB grayed out if FieldA for the current record is Yes. If I select a record where FieldA equals No, FieldB would not gray out. All this appears to work just as I needed.

    Here's where things look a bit odd. Imagine I have three records (Rec1, Rec2, Rec3). Rec1 and Rec3 have a FieldB value of Yes so FieldA for those two records are grayed out. Rec2's FieldB value is No, hence FieldA is not grayed out. If I select FieldB on Rec3 to set it to No (uncheck the box), then Rec1 and Rec2's FieldA both gray out. If I select Rec1 and Rec2, they both update and look OK. I close the form and reopen it. I select each record again, so now Rec2 and Rec3 have FieldB set to No while Rec1's FieldB is Yes. Now the only record with FieldA grayed out is Rec1. I click on Rec3's FieldB field to set to Yes (check the box), now Rec1's FieldA is no longer grayed out. When I click into the Rec1 record it will update and gray FieldA again.

    What appears to be happening is whichever record I'm updating by checking / unchecking FieldB applies it's logic across all the records. Again, I hope that makes sense as to the sequence of events.

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Disable grid field based on other field value

    All flags on all records? What do you mean?

    So far we were talking about the property of of the data source field, which is only one. It doesn't matter whether you have zero or a million records; the form has a just single definition of the data source and its fields. The same is true for the grid column - it has just a single definition.

    Can elaborate what problem you have with the solution discussed above?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable grid field based on other field value

    Hi Martin,

    That makes sense. Adding the code in the active() method does appear to check / set the fields I need. However, I'm seeing this odd reaction when I change the value of TBL.FieldB on a record. Once I do that, it resets all the enabled flag of all the records based on the changed record. There must be something I've overlooked.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Disable grid field based on other field value

    No, active() is the right method and it's widely used for your kind of scenario. It just work differently than you thought.

    Allowing and disabling editing make no sense for other records than the selected one, because if you want to edit another record, you have to select it first, which is when active() triggers and runs your logic.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable grid field based on other field value

    Hi Martin,

    Yes, seems like active() is probably the wrong call. I tried init() which didn't appear to be the correct method call either.

    And, thank you for the suggestion on the code refactor. It certainly looks much cleaner and readable.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Disable grid field based on other field value

    Hi Andre,

    Leave() and leaveRecord() seem to just do the opposite of what I was seeing before with active(). What I'd like to do is have all this logic run per record as the grid loads.

    Thank you.

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Disable grid field based on other field value

    active() method is indeed the right place, and your code looks all right too.

    Are you aware of that active() executes when you select a record? If you open the form, it executes for the first record, because it's selected (and it's the only record you can edit), if you switch to another record, active() executes for it, and so on.

    It's off topic, but I would refactor your code not to repeat the field selection. For example:

    boolean enableA = tbl.FieldB == NoYes::No;
    tbl_ds.object(fieldNum(TBL, FieldA)).allowEdit(enableA);
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,253 Super User 2024 Season 2 on at
    RE: Disable grid field based on other field value

    Hi DHeisman,

    You can try to use the coding on the leave() or leaveRecord() method of the datasource.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans