Retrieving Current Records in Dynamics 365 Finance and Operations with Chain of Commands (CoC)

Retrieving Current Records in Dynamics 365 Finance and Operations with Chain of Commands (CoC)

In Microsoft Dynamics 365 Finance and Operations (D365 F&O), Chain of Commands (CoC) offers a powerful way to extend standard or custom functionalities without modifying base code. A common task for D365 developers is fetching the current record within the form context. Here’s a quick guide to retrieving the current record using Chain of Commands across various elements like

  • FormDataSource
  • Form
  • FormDataField
  • FormControl
  • Table


1. Fetching the Current Record from FormDataSource

The FormDataSource class provides a convenient way to access the current record from the form's data source. By leveraging CoC, you can directly reference the current record using this.

Using this

[ExtensionOf(formDataSourceStr(SalesTable, SalesTable))]
public final class SalesTable_Extension
{
    public void someMethod()
    {
        next someMethod();
        SalesTable salesTable = this.cursor(); // current record
    }
}        

Using formRun

[ExtensionOf(formDataSourceStr(SalesTable, SalesTable))]
public final class SalesTable_Extension
{
    public void someMethod()
    {
         
        FormRun formRun = this as FormRun;
        FormDataSource formDataSource = formRun.datasource(FormDatasourceStr(ProjInvoiceListPage, ProjInvoiceJour)); // get data source
        ProjInvoiceJour projInvoiceJour = formDataSource.cursor(); // current record

  next someMethod();
    }
}        

2. Fetching the Current Record from Form

If you need to access the current record from the Form class directly, you can cast the form instance to FormRun and fetch the data source for the target table.

[ExtensionOf(formStr(SalesTable))]
public final class SalesTable_FormExtension
{
    public void someMethod()
    {
        FormRun formRun = this as FormRun;
        FormDataSource ProjInvoiceJour_ds = formRun.datasource(FormDatasourceStr(ProjInvoiceListPage, ProjInvoiceJour)); // get datasource
        ProjInvoiceJour projInvoiceJour = ProjInvoiceJour_ds.cursor(); // get current record
next someMethod();
    }
}        

3. Fetching the Current Record from FormDataField

Using FormDataField, you can retrieve the current record by accessing the field’s data source. Here’s how to do it:

[ExtensionOf(formDataFieldStr(SalesTable, SalesLine, ItemId))]
public final class SalesLine_FormDataFieldExtension
{
    public void someMethod()
    {
        FormDataObject formDataObject = any2Object(this) as FormDataObject;
        FormDataSource formDataSource = formDataObject.dataSource();
        SalesLine salesLine = formDataSource.cursor(); // current record

        next someMethod();
    }
}        

4. Fetching the Current Record from FormControl

When dealing with FormControl, such as a button, you can access the current record through the form's data source. Here are two options:

[ExtensionOf(formControlStr(SalesTable, Complete))]
public final class SalesTable_FormControlExtension
{
    public void someMethod()
    {
        FormButtonControl formButtonControl = any2Object(this) as FormButtonControl;
        FormDataSource formDataSource = formButtonControl.formRun().dataSource(tableStr(SalesTable));
        SalesTable salesTable = formDataSource.cursor(); // current record

        next someMethod();
    }
}        

Or alternatively:

public void someMethod()
{
    FormRun formRun = this.formRun();
    FormDataSource formDataSource = formRun.dataSource("SalesTable");
    SalesTable salesTable = formDataSource.cursor(); // current record

    next someMethod();
}        

5. Fetching the Current Record from Table

Directly referencing the table is straightforward. Using this keyword, you can easily access the current record:

[ExtensionOf(tableStr(SalesTable))]
public final class SalesTable_Extension
{
    public void someMethod()
    {
        SalesTable salesTable = this; // current record

        next someMethod();
    }
}        

As a beginner in Dynamics 365, I am continually learning and open to any feedback on this approach. If there are better practices or suggestions for improvement, I’d be grateful for any insights!


#Dynamics365 #FinanceAndOperations #MicrosoftDynamics365 #D365FO #XPlusPlus #ChainOfCommands #D365Developer #ERPDevelopment #MicrosoftERP #TechnicalConsultant #CodingBestPractices #LearningAndDevelopment #BeginnerTips #CareerInTech #Microsoft

Venkatesh Orusu

Associate Technical Consultant | MCP | MB-500 | Microsoft Certified: Dynamics 365 Developer

4 个月

Interesting

Srinivas Y Keshaboina

Director of Microsoft Practice, Argano India

4 个月

Great beginning Dinesh. Keep more coming !!!

要查看或添加评论,请登录

Dinesh Srinivas Gadisetti的更多文章

社区洞察

其他会员也浏览了