Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to disable editing...
Finance forum
Suggested answer

How to disable editing on all form datasources?

Posted on by 686
Hi, I would like to disable editing on a form after a specific condition is met. I have already tried this code, but unfortunately without results. Can someone help?
 
[FormDataSourceEventHandler(formDataSourceStr(PurchReqTable, PurchReqTable), FormDataSourceEventType::Activated)]
public static void PurchReqTable_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
{
    FormDataSource purchReqTable_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, PurchReqTable));
    FormDataSource purchReqLine_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, PurchReqLine));
    FormDataSource inventDim_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, InventDim));
    FormDataSource businessJustificationHeader_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, BusinessJustificationHeader));
    FormDataSource businessJustificationLine_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, BusinessJustificationLine));
    FormDataSource logisticsPostalAddressLine_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, LogisticsPostalAddressLine));
    FormDataSource purchReqLineKMCollection_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, PurchReqLineKMCollection));
    FormDataSource purchReqExternalSource_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, PurchReqExternalSource));
    FormDataSource purchReqLineTaxExtensionIN_ds = sender.formRun().dataSource(formDataSourceStr(PurchReqTable, PurchReqLineTaxExtensionIN));


    PurchReqTable purchReqTable = purchReqTable_ds.cursor();

    if(purchReqTable.myCondition())
    {
        purchReqTable_ds.allowEdit(false);
        purchReqLine_ds.allowEdit(false);
        inventDim_ds.allowEdit(false);
        businessJustificationHeader_ds.allowEdit(false);
        businessJustificationLine_ds.allowEdit(false);
        logisticsPostalAddressLine_ds.allowEdit(false);
        purchReqLineKMCollection_ds.allowEdit(false);
        purchReqExternalSource_ds.allowEdit(false);
        purchReqLineTaxExtensionIN_ds.allowEdit(false);

    }
}
 
  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 290,435 Super User 2024 Season 2 on at
    How to disable editing on all form datasources?
    Hi Shooowtek,

    As mentioned by others, some standard logic might overwrite your coding. During debugging you should not only focus on your own coding, but also what happens after that. 

    What is the exact condition when the data sources should be read-only? Depending on the complexity there can be another way to achieve your requirement by developing a security policy to manage CUD actions.
  • Suggested answer
    Waed Ayyad Profile Picture
    Waed Ayyad 5,305 Super User 2024 Season 2 on at
    How to disable editing on all form datasources?
    Hi,
     
    Try to use active method through COC, does you condition depend on certain changes on the form itself? Try to add your changes to the init method of the form also.
     
     
    Thanks,
    Waed Ayyad
    If this helped, please mark it as "Verified" for others facing the same issue
     
  • Suggested answer
    Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,548 Moderator on at
    How to disable editing on all form datasources?
    This activated event gets called from super() of active method. But I feel, the standard is overriding your logic. So I would suggest you to write code post next of active method.
  • Suggested answer
    AtulMP Profile Picture
    AtulMP 4 on at
    How to disable editing on all form datasources?

    Hello,

    It seems you're using allowEdit(false) based on certain conditions in your form's data source, likely through an event handler. Instead of relying on event handlers, you can simplify your approach by directly overriding the form methods.

    For example, you can set the edit permission like this:

    yourdatasource_ds.allowEdit(false);

    Or, for a specific field, you can use:

    yourdatasource_ds.object(fieldNum(datasourcetable, field)).allowEdit(false);

    This approach reduces complexity and keeps your code organized. For further insights, you may find this blog helpful: Enable/AllowEdit of DataSource Field.

    If you have any questions or need more information, feel free to ask!

  • Shooowtek Profile Picture
    Shooowtek 686 on at
    How to disable editing on all form datasources?
    Yes, I debugged the code and my condition is true. I can't find the reason why disabling editing doesn't work.
  • Anton Venter Profile Picture
    Anton Venter 18,495 Super User 2024 Season 2 on at
    How to disable editing on all form datasources?
    Hello,
     
    Is your condition actually true? Perhaps the standard code is overriding your change after your code has executed. Have you debugged your code to see what's going on?

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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,435 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,317 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans