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

form data source code

(3) ShareShare
ReportReport
Posted on by 1,845
Hi,


When do we put setField1Access() method on form level? and when do we put it in formdatasource level? [ i mean in general do we control fields being editable on form or datasource levels?]
I feel since the fields are related to datasource then they should be written in datasource level, but i can see lots of standard code where code is written in form level
 
Categories:
I have the same question (0)
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,164 Super User 2025 Season 2 on at
    form data source code
    Hi,
     
    So could we say as a rule of thumb, if a condition would affect different datasources, then i put the access method in form level and if it's only related to the datasource, then i put the access method in the form datasource level? or always in form level?
     
    Depends on the scenario in place. If you are looking to re-use code then the suggestion is to add to the form level. If the access method for a field is to be decided at only a specific data source level then better to write the same code at the data source level. 
     
    In your example, write one method for setFieldAccess and retrieve the values through the active() method. !Table1.Field2 is having blank values hence the condition fails. Trying passing the form DataSource value as a parameter in the setFieldAccess and retrieve !Table.Field2 value. It should work. 
     
    Hope this helps. Happy to answer questions, if any.
     
  • Martin Dráb Profile Picture
    235,966 Most Valuable Professional on at
    form data source code
    Of course, it's possible to split the logic to several methods rather than having it centralized.
     
    The error doesn't mean that the approach is wrong, you just have a bug in setting the value. Even if it compiled, it's logically wrong, because you're trying to refer to the current record of Table1 data source when creating the form, before any data is loaded to the data source. To get the correct behavior, you'd need to check the condition and update the variable every time when Table1 changes, i.e. in active(), or - which is easier - use a method instead of a variable.
  • .. Profile Picture
    1,845 on at
    form data source code
    Hi @Martin Dráb,

    No i meant sth like this:
    to define global variable and use this variable in different datasource methods
     
    [ExtensionOf(formStr(Table1))]
    final class Table1Form_Extension
    {
        public boolean condition = !Table1.Field2;
    
    }
    
    [ExtensionOf(formDataSourceStr(Form1, Table1))]
    final class From1Table1DataSource_Extension
    {
        public int active()
        {
            int ret;
     
            ret = next active();
     
            this.setField1Access();
     
            return ret;
        }
     
        public void setField1Access()
        {
            this.object(fieldNum(Table1, Field1)).allowEdit(condition);
        }
     
    }
    
    
    [ExtensionOf(formDataSourceStr(Form1, Table2))]
    final class From1Table2DataSource_Extension
    {
        public int active()
        {
            int ret;
     
            ret = next active();
     
            this.setFieldXAccess();
     
            return ret;
        }
     
        public void setFieldXAccess()
        {
            this.object(fieldNum(Table2, FieldX)).allowEdit(condition);
        }
     
    }

    But I got errors on the boolean "condition" (not sure if i did it wrongly or if it's not doable)
    The qualifier 'Table1' is not valid in this context. Use a variable of this type instead.    
  • Martin Dráb Profile Picture
    235,966 Most Valuable Professional on at
    form data source code
    You could put it on a data source, but the logic is related to two data sources, therefore neither of them is the logical place where to do it. 
  • .. Profile Picture
    1,845 on at
    form data source code

    In your example, couldn't I for example define the boolean variable as global and keep the method in datasource level?
     

    Yes i was able to see standard examples where they put the access method in form level, and some examples where they put the method in form datasource level.

    So could we say as a rule of thumb, if a condition would affect different datasources, then i put the access method in form level and if it's only related to the datasource, then i put the access method in the form datasource level? or always in form level?
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,164 Super User 2025 Season 2 on at
    form data source code
    Hi,
     
    As Martin mentioned you can set the field properties for multiple data sources under setFieldAccess method and invoke them from your init() method. Check out this example in the CustTable form. Easier to manage your code if you have setFieldAccess at the form level for multiple fields. 
     
     
     
    Looking at the requirement the approach suggested by Martin is better. Have one method and invoke that method through different data sources. Also, keep the context of the record in mind, if applicable.
     
  • Martin Dráb Profile Picture
    235,966 Most Valuable Professional on at
    form data source code
    A single condition can easily influence fields from different data sources. For example:
     
    void setFieldAccess()
    {
        // Our condition
        boolean enabled = false;
      
        // Fields from two data sources
        TableA_ds.object(fieldNum(TableA, X)).allowEdit(enabled);
        TableB_ds.object(fieldNum(TableB, Y)).allowEdit(enabled);  
    }
  • .. Profile Picture
    1,845 on at
    form data source code
    Hi @Martin Dráb,

    How would the same condition influence fields from several datsources?

    so currently my method makes 'field1' editable if 'field2' is false. How would another datasource want to call the same method?
     
    I'm just trying to think of a code where i will need to put this method in form level instead, and let two datasources call it
  • Suggested answer
    Martin Dráb Profile Picture
    235,966 Most Valuable Professional on at
    form data source code
    No, there isn't anything like a data source field applicable to the whole form. A data source field belongs to a data source and you're changing the state of the same object regardless of whether you call your code from the form or the data source.
     
    There is no difference in behavior, just in the structure of code. It's quite common to concentrate the logic to a common method on the form, especially if the same condition influences fields from several data sources.
  • Suggested answer
    Navneeth Nagrajan Profile Picture
    2,164 Super User 2025 Season 2 on at
    form data source code
    Hi,
     
    allowEdit() on formDataSource means controlling the disablement or enablement of the field only at the table level that serves as the primary or the related datasource which in this case is Table1. 
     
    However, allowEdit on the form level means it references the entire context of the form, i.e. the editable properties applied on the field are applicable at the entire form level. It also means if the field resides in a related datasource there could be conditions based on which the field can be editable or non-editable. This overrides the editable state of all controls and datasources on the form unless we override it again at a more specific level like control level or data source level.
     
    Hope this helps. Happy to answer questions, if any.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
Sohaib Cheema Profile Picture

Sohaib Cheema 823 User Group Leader

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 491 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans