web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Disable a field based on second field value in AX 2012 R3.

(0) ShareShare
ReportReport
Posted on by 1,260

Hi all,

I have a scenario where I have to disable a field if the second field value is yes.

I have two fields created in InventTabble table i.e.

- String type (Manufactur ID)

- Enum type (MID per batch)

The default value for "MID per batch" is No.

I have to show this fields in the "EcoResProductDetailsEntended" form based on a condition i.e.

- if "MID per batch" = Yes then disable "Manufactur ID) and,

- if "InventItemSetupSupplyType.DefaultOrderType <> "Purchase Order" then disable "MID per batch" and "Manufactur ID" fields.

I am using the modified( ) method under the form method and code used is -

public void modified( )
{
   if(InventTable.MIDPerBatch == NoYes::Yes)
   {
     InventTable_ds.object(fieldnum(InventTable, ManufacturID)).allowEdit(false);
   }
   else
   {
     InventTable_ds.object(fieldnum(InventTable, ManufacturID)).allowEdit(true);
   }
   
   if(InventItemSetupSupplyType.DefaultOrderType <> "Purchase Order")
   {
     InventTable_ds.object(fieldnum(InventTable, MIDPerBatch)).allowEdit(false);
     InventTable_ds.object(fieldnum(InventTable, ManufacturId)).allowEdit(false);
   }
   else
   {
     InventTable_ds.object(fieldnum(InventTable, MIDPerBatch)).allowEdit(true);
     InventTable_ds.object(fieldnum(InventTable, ManufacturId)).allowEdit(true);
   }
}


But I am still not able to perform the requirement - am I doing wrong or should I not be using the modified( ) method or am I calling the method under wrong form level. I tried to create the method under the data source of InventTable method but still no result.

How can I disable the fields based on the other field conditions?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    236,320 Most Valuable Professional on at
    RE: Disable a field based on second field value in AX 2012 R3.

    First of all, let me fix your code; especially the comparison isn't even syntactically correct. I've also simplified the code, all duplicate code would be difficult to read and maintain:

    boolean defaultOrderIsPurch = inventItemSetupSupplyType.DefaultOrderType == ReqPOType::Purch;
    
    FormDataObject midPerBatchField  = InventTable_ds.object(fieldnum(InventTable, MIDPerBatch));
    FormDataObject manufacturIdField = InventTable_ds.object(fieldnum(InventTable, ManufacturId));
    
    midPerBatchField.allowEdit(inventTable.MIDPerBatch == NoYes::No && defaultOrderIsPurch);
    manufacturIdField.allowEdit(defaultOrderIsPurch);

    You need to handle two cases:

    1. When you get into a record without changing anything. Obviously modified() isn't useful in this case; you need active() method of the datasource.
    2. When some values changes. Your logic depends on fields from two tables (InventItemSetupSupplyType and InventTable), but let's assume that InventItemSetupSupplyType is constant, so it's only about changing MIDPerBatch. That's where modified() comes handy.

    To cover both cases without duplicating code, extract the logic to separate method and call it from both active() and modified().

  • Martin Dráb Profile Picture
    236,320 Most Valuable Professional on at
    RE: Disable a field based on second field value in AX 2012 R3.

    @Crispin: Wouldn't calling active() drop all changes done to the record? I think you would have to save the record before calling active(), but it's likely not wanted and it's sometimes even impossible (such as when you're creating a new record and all mandatory fields are not filled in yet), therefore I wouldn't recommend that.

  • Daljit Kaur Rahsi Profile Picture
    1,260 on at
    RE: Disable a field based on second field value in AX 2012 R3.

    Thanks Martin for the simplifying the code and suggesting to use the active() method -

    I changed the lines as per requirements below and it works correctly. Thank again -

    InventItemSetupSupplyType inventItemSetup;
    boolean defaultOrderIsPurch;
    FormDataObject midPerBatchField;
    FormDataObject manufacturIdField;
    EcoResTrackingDimensionGroupFldSetup trackingDimensions;
    EcoResTrackingDimensionGroupItem trackingItem;
    boolean isActive;
    
    defaultOrderIsPurch = inventItemSetup.DefaultOrderType == ReqPOType::Purch;
    isActive = trackingDimensions.IsActive == NoYes::Yes;
    
    midPerBatchField  = InventTable_ds.object(fieldnum(InventTable, MIDPerBatch));
    manufacturIdField = InventTable_ds.object(fieldnum(InventTable, ManufacturerId));
    
    midPerBatchField.allowEdit(defaultOrderIsPurch && InventTable.PmfProductType != PmfProductType::BOM
    && EcoResTrackingDimensionGroupItem.TrackingDimensionGroup ==5637144576 );
    
    manufacturIdField.allowEdit(defaultOrderIsPurch && InventTable.PmfProductType != PmfProductType::BOM
    && InventTable.LSH_MIDPerBatch == NoYes::No );


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

#2
NNaumenko Profile Picture

NNaumenko 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans