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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Set Form datasource read only conditionally

(0) ShareShare
ReportReport
Posted on by 230

Good morning all,

This question is regarding the Salestable form.

We have an intercompany setup. When a salesline is delivered in the supply chain company, I would like to make the salesline readonly in both the supply chain company as the sales company.

I tried to add the following code (COC extension of the salesline formdatasource):


[ExtensionOf(formDataSourceStr(SalesTable, SalesLine))]
final class ELCWDSalesTableSalesline_Extension
{


public void init()
    {
        switch (element.args().dataset())
        {
            case tableNum(Salesline) :
                Salesline       localSalesline = element.args().record();
                SalesLine       saleslineSCNL;
                SalesStatus     salesstatus;
               
                if(localSalesline.isInterCompanyOrder()==false)
                {   
                    ELCWDOnHandInfoClass    onhandclass;
                    DataAreaId              sccompany; 
                    Purchline               localpurchline;
                                
                    //Get Supply Chain Company DataAreaId
                    onhandclass = new ELCWDOnHandInfoClass();
                    sccompany = onhandclass.returnSCCompany();
                    
                    select firstonly localpurchline where localpurchline.InventRefTransId == localSalesline.InventTransId;
                    
                    changecompany(sccompany)
                    {
                        select firstonly saleslineSCNL where saleslineSCNL.SalesId==localSalesline.salesid && saleslineSCNL.inventtransid == localpurchline.InterCompanyInventTransId;
                        salesstatus=saleslineSCNL.SalesStatus;
                    }
                    
                    if(salesstatus != SalesStatus::Backorder || salesstatus != SalesStatus::None)
                    {
                        allowEditFieldsOnFormDS_W(element.dataSource(formDataSourceStr(SalesTable, Salesline)), false);
                        allowEditFieldsOnFormDS_W(element.dataSource(formDataSourceStr(SalesTable, SalesTable)), false);
                        allowEditFieldsOnFormDS_W(element.dataSource(formDataSourceStr(SalesTable, InventDim)), false);
                        allowEditFieldsOnFormDS_W(element.dataSource(formDataSourceStr(SalesTable, WHSLoadLine)), false);
                        allowEditFieldsOnFormDS_W(element.dataSource(formDataSourceStr(SalesTable, WHSSalesLine)), false);
                    }
                }
                break;

            case tableNum(salestable) :
                salestable       localSalestable = element.args().record();
                
                //For testing/dubugging purposes
                info(localSalestable.salesid);
                
                break;
        }

        next Init();
    }
    
    }

It seems args gets a SalesTable dataset.

Also: the dataset does not update.

As a second option I looked at the Salesline datasources' Active method, but this method seems not te be extendable with COC.

What am I missing here? I hope someone can put me in the right direction.

I have the same question (0)
  • Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Willem,

    I am not sure if init would be the right place for this, since you are setting the datasource as not editable based on a value in SalesLine. Since you are not able to create a CoC for active method, you can try creating an event handler for the "OnActivated" event under SalesLine datasource and check if that works for you.

  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at

    What kind of problem do you have with CoC on active()? I didn't notice any.

  • Willem van Duren Profile Picture
    230 on at

    @Martin: It says that this method is not available for COC.

    @Gunjan: Yes. That works. Now I get the salesline datasource. However: the form datasource remains editable. I used the debugger, and all code is reached, but the datasource is not blocked. I used the following code:

    public static class ELCWDSalesTableSalesline_Extension
    {
        /// 
        ///
        /// 
        /// 
        /// 
        [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
        public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
        {
            FormRun formRun= sender.formRun();
            Object salesline_ds = formRun.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;
            SalesLine localSalesline = salesline_ds.cursor();
            
            if(localSalesline.isInterCompanyOrder()==false)
                    {   
                        ELCWDOnHandInfoClass    onhandclass;
                        DataAreaId              sccompany; 
                        Purchline               localpurchline;
                        salesline               saleslineSCNL;
                        salesstatus salesstatus;
                        //Get Supply Chain Company DataAreaId
                        onhandclass = new ELCWDOnHandInfoClass();
                        sccompany = onhandclass.returnSCCompany();
                        
                        select firstonly localpurchline where localpurchline.InventRefTransId == localSalesline.InventTransId;
                        
                        changecompany(sccompany)
                        {
                            select firstonly saleslineSCNL where saleslineSCNL.SalesId==localSalesline.salesid && saleslineSCNL.inventtransid == localpurchline.InterCompanyInventTransId;
                            salesstatus=saleslineSCNL.SalesStatus;
                            info(strFmt("Salesstatus : %1",salesstatus));
                        }
                        
                        if(salesstatus != SalesStatus::Backorder || salesstatus != SalesStatus::None)
                        {
                            salesLine_ds.allowEdit(false) ;
                        }
                    }
    
        }
    
    }

  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at

    I don't have any such problems with active(). I even explicitly tested it with the same data source , and it compiles without any error.

    Maybe you're running on quite an old version of F&O. Consider keeping it up to date and benefit from all available features.

    By the way, you can replace this:

    FormRun formRun= sender.formRun();
    Object salesline_ds = formRun.dataSource(formDataSourceStr(salestable, SalesLine)) as FormDataSource;
    SalesLine localSalesline = salesline_ds.cursor();

    with this:

    SalesLine localSalesline = sender.cursor();

  • Willem van Duren Profile Picture
    230 on at

    Hi Martin!

    I think I did something wrong in that case.  My DEV is running 10.0.13 so it shouldn't be a problem.

    Does it have advantages to use the Active method over the event handler ? Because the dataset works now.

  • Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Willem,

    You don't need the "salesLine_ds" variable. You can use "sender" instead.

    Please try with "sender.allowEdit(false) and check if that works.

  • WillWU Profile Picture
    22,363 on at

    Hi Willem van Duren,

    Please try to restart VS,  rebuild your project and sync the database.

    COC is nearly the same as event handler,  COC supports return value and parameter modification of the extended method in a much more readable manner.

  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at

    No, it's not necessary to rewrite it with CoC if you're happy with the event handler. They both do the same job.

    I typically prefer CoC, because the code is usually simpler, but there are cases when I use event handlers. Most importantly if I want a single handler for multiple events, but also if I don't want to end up with too many extension classes for the same form. Rather then creating, say, two DS extensions and three form control extensions, I use handlers and put them all to a single class.

  • Willem van Duren Profile Picture
    230 on at

    Ok, tried rebuild, sync etc. No luck.

    Changed the code to only disable the DS:

    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesLine), FormDataSourceEventType::Activated)]
    public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
    SalesLine localSalesline = sender.cursor();
    sender.allowEdit(false) ;
    }

    Even in this case the datasource remains editable.

  • Martin Dráb Profile Picture
    238,745 Most Valuable Professional on at

    Your code is trying to set AllowEdit on the table buffer, but it should be on the form data source:

    public static void SalesLine_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
    	sender.allowEdit(false);
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 558 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 402

#3
Diego Mancassola Profile Picture

Diego Mancassola 261

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans