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 :
Finance | Project Operations, Human Resources, ...
Suggested answer

can we make field enable/disable in form extension

(1) ShareShare
ReportReport
Posted on by 1,836
I was trying to make field on the form enable disable using event handler is this possible i was trying to do so using onactivated event handler but it is not getting done 
can any on guide me on this how can i do this 
I have the same question (0)
  • Martin Dráb Profile Picture
    236,867 Most Valuable Professional on at
    can we make field enable/disable in form extension
    Yes, it is possible.
     
    Can you please show us your code and tell us what information you collected when debugging it?
     
    Also, is it an unbound control or is it bound to a data source?
  • Dineshkarlekar Profile Picture
    1,836 on at
    can we make field enable/disable in form extension
    hi martin thanks for reply i have made the form extension and made table extension and added the field on that table which i have later added on form which should be get enable disable on the enum value my code is below i want to make the field editable only when the projectworkbreakdownstructureviewmode::costestimtes    my field project value is bound to data source and i have set auto declaration to yes can you plz guide me on it where i am getting wrong.
    [ExtensionOf(formDataSourcestr(ProjWorkBreakdownStructureV2,PSAActivityEstimates))]
    final class DT_PSAActivityEstimatesDK_Extension
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [FormDataSourceEventHandler(formDataSourceStr(ProjWorkBreakdownStructureV2, PSAActivityEstimates), FormDataSourceEventType::Activated)]
        public static void PSAActivityEstimates_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
        {
            
            //Getting formrun from sender to retrieve form controls
            FormRun               formRun              = sender.formRun();
     
            //Retrieving Current record from data source
            PSAActivityEstimates  pSAActivityEstimates = sender.cursor();
    
            //Based on our condition we can perform our operations on //control.
            if (ProjWorkBreakdownStructureViewMode::Scheduling)
            {
                pSAActivityEstimates.object(fieldNum(pSAActivityEstimates, ProjectValue)).allowedit(false);
            }
            else
                if(ProjWorkBreakdownStructureViewMode::CostEstimates)
            {
                pSAActivityEstimates.object(fieldNum(pSAActivityEstimates, ProjectValue)).allowedit(true);
            }
    
        }
        
    
    
    }
     
     
  • Suggested answer
    Martin Dráb Profile Picture
    236,867 Most Valuable Professional on at
    can we make field enable/disable in form extension
    First of all, compile your code and fix compilation errors.
     
    Then you should have used the debugger to find out where your code doesn't behave as expected. You'd find that your 'if' condition always returns the same value, regardless of what you have in pSAActivityEstimates variable. Then it would be easy to spot that you forgot to use pSAActivityEstimates in your condition; you currently check whether the enum value is zero
    or not, instead of comparing the enum value with a field value.
     
    When I fix the problem, throw away unused code and remove the code duplication, I end up with this:
    [FormDataSourceEventHandler(formDataSourceStr(ProjWorkBreakdownStructureV2, PSAActivityEstimates), FormDataSourceEventType::Activated)]
    public static void PSAActivityEstimates_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
        //Retrieving Current record from data source
        PSAActivityEstimates pSAActivityEstimates = sender.cursor();
    
        boolean allowEditProjectValue = pSAActivityEstimates.TheField == ProjWorkBreakdownStructureViewMode::CostEstimates;
        
        pSAActivityEstimates.object(fieldNum(PSAActivityEstimates, ProjectValue)).allowEdit(allowEditProjectValue);
    }
  • Dineshkarlekar Profile Picture
    1,836 on at
    can we make field enable/disable in form extension
     ClassDoesNotContainMethod: Table 'PSAActivityEstimates' does not contain a definition for method 'object' and no extension method 'object' accepting a first argument of type 'PSAActivityEstimates' is found on any extension class.  
     
    i tried with your code but getting this error should i put form datasource there 
  • Martin Dráb Profile Picture
    236,867 Most Valuable Professional on at
    can we make field enable/disable in form extension
    Oh, that's one more compilation error in your code. You're using a table buffer where you need a data source. You have it in sender object.
  • Dineshkarlekar Profile Picture
    1,836 on at
    can we make field enable/disable in form extension
    i have added form datasource but getting the error " object refrence is not set to an instance of object "
      PSAActivityEstimates pSAActivityEstimates = sender.cursor();
            FormDataSource   pSAActivityEstimates_ds ;
    
            boolean allowEditProjectValue = pSAActivityEstimates.ProjectValue== ProjWorkBreakdownStructureViewMode::CostEstimates;
        
            pSAActivityEstimates_ds.object(fieldNum(PSAActivityEstimates, ProjectValue)).allowEdit(allowEditProjectValue);
     
  • Martin Dráb Profile Picture
    236,867 Most Valuable Professional on at
    can we make field enable/disable in form extension
    The error is completely correct - you're trying to call a method on an empty object, because you've never put any value to your pSAActivityEstimates_ds variable.
  • Dineshkarlekar Profile Picture
    1,836 on at
    can we make field enable/disable in form extension
    i have did beleow code but the field is uneditable in both condition not getting it why , i hade tried with adding and removing PSAActivityEstimates.ProjectValue in boolean but not working 
      FormRun fr = sender.formRun();
            FormDataSource   pSAActivityEstimates_ds = fr.dataSource('PSAActivityEstimates');
            PSAActivityEstimates pSAActivityEstimates = sender.cursor();
           
                boolean allowEditProjectValue  = ProjWorkBreakdownStructureViewMode::Scheduling;
    
            pSAActivityEstimates_ds.object(fieldNum(PSAActivityEstimates, ProjectValue)).allowEdit(allowEditProjectValue);
     
  • Martin Dráb Profile Picture
    236,867 Most Valuable Professional on at
    can we make field enable/disable in form extension
    Now you re-introduced the bug I explained and fixed before. You're wrong in believing that you have two cases. In fact, the value of your allowEditProjectValue variable is hard-coded; it'll never have a different value and it's completely unrelated to the data in the table.
     
    Please walk through your code in debugger to see that I'm right, if you don't see just by looking at code.
     
    Also, fr.dataSource('PSAActivityEstimates') seems to be just an uglier way to get what you already have in sender variable.
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    3,634 Moderator on at
    can we make field enable/disable in form extension
    boolean allowEditProjectValue  = ProjWorkBreakdownStructureViewMode::Scheduling;
    This means, this enum has always Scheduling value which means it returns always true. If you want to give a condition, then you need to write something like,
    boolean   allowEditProjectValue = (fieldName == ProjWorkBreakdownStructureViewMode::Scheduling);
    Or fieldName == ProjWorkBreakdownStructureViewMode::Scheduling can be put in place of true or false. In that case it will check if that field is having this value or not.
     

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 734 Super User 2025 Season 2

#2
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 636

#3
Martin Dráb Profile Picture

Martin Dráb 553 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans