Saturday, May 21, 2022

Using Extension Chain of Command (COC) and EventHandlers in D365FO

 1. DataSource Extension class:

[ExtensionOf(formDataSourceStr(InventQualityOrderTable, InventQualityOrderLine)) ]

final class AxInventQualityOrderTableFormDataSource_Extension

{

    public void initValue()

    {

        FormDataSource formDS = this;

        InventQualityOrderLine  qualityOrderLine = formDS.cursor();

        

        //OR you can directly do this like

        InventQualityOrderLine qualityOrderLine = this.cursor();

        next initValue();

        qualityOrderLine.VariableId     = 'Test';

    }

}


2. FormDataSourceField Extension Class

[ExtensionOf(formDataFieldStr(InventQualityOrderTable, InventQualityOrderLine, TestId)) ]

final class AxInventQualityOrderTableFormDataSourceField_Extension

{

    public void modified()

    {

        FormDataObject      formDataObject = any2Object(this) as FormDataObject;    

        FormDataSource      formDS = formDataObject.datasource();

        InventQualityOrderLine  qualityOrderLine = formDS.cursor();

        //OR you can merge above two lines into single line like this;

        InventQualityOrderLine qualityOrderLine = formDataObject.datasource().cursor();

        next modified();

        // do your logic here

        qualityOrderLine.VariableId  = 'Assign value';

    }

}


3. Form Button COC Class Extension

[ExtensionOf(formControlStr(LedgerJournalTransCustPaym, ButtonSettlement))]

final class AxLedgerJournalTransCustPaymFormButton_Extension

{

    public void clicked()

    {  

        FormButtonControl       btnCtrl = any2Object(this) as FormButtonControl;

        FormDataSource          ds = btnCtrl.formRun().dataSource(tableStr(LedgerJournalTrans));

        LedgerJournalTrans      ledgerJournalTrans = ds.cursor();

        

        //do your logic here

        info(strFmt("%1", ledgerJournalTrans.Voucher));

        next clicked();

        //Here 

    }

}


4. Form Datafield OnModified EventHandler

[FormDataFieldEventHandler(formDataFieldStr(InventQualityOrderTable, InventQualityOrderLine, IncludeResults), FormDataFieldEventType::Modified)]

public static void IncludeResults_OnModified(FormDataObject sender, FormDataFieldEventArgs e)

{

    InventQualityOrderLine      qualityOrderLine = sender.datasource().cursor();

    info(strFmt("%1", qualityOrderLine.TestId));

}


5. Form control modified eventHandler

[FormControlEventHandler(formControlStr(InventQualityOrderTableCreate, InventoryDimensions_InventSiteId), FormControlEventType::Modified)]

public static void InventoryDimensions_InventSiteId_OnModified(FormControl sender, FormControlEventArgs e)

{

    FormRun                     formRun = sender.formRun();

    InventQualityOrderTable     inventQualityOrderTable   = formRun.dataSource(tableStr(InventQualityOrderTable)).cursor();

    InventDim                   inventDim                 = formRun.dataSource(tableStr(InventDim)).cursor();

    info(StrFmt("%1", inventDim.InventSiteId));


}


6. Post Event Handler 

[PostHandlerFor(tableStr(PurchTable), tableMethodStr(PurchTable, initValue))]

public static void PurchTable_Post_initValue(XppPrePostArgs args)

{

    PurchTable purchTable = args.getThis() as PurchTable;

    purchTable.test_PrintNote=NoYes::Yes;

}


7. Form DataSource OnActivated EventHandler

[FormDataSourceEventHandler(formDataSourceStr(CustTable, CustTable), FormDataSourceEventType::Activated)]

public static void CustTable_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)

{

     CustTable           custTable     = sender.cursor();

     FormDataSource      custTable_ds  = sender.formRun().dataSource("CustTable");

     FormRun             element       = sender.formRun();

     FormControl         myNewButton   = element.design(0).controlName("MyNewButton");

     myNewButton.enabled(false);

}


8. Table OnInserted EventHandler

[DataEventHandler(tableStr(CustInvoiceTrans), DataEventType::Inserted)]

public static void CustInvoiceTrans_onInserted(Common sender, DataEventArgs e)

{

    CustInvoiceTrans custInvoiceTrans = sender as CustInvoiceTrans;

    SalesTable salesTable = SalesTable::find(custInvoiceTrans.SalesId);

    if(salesTable.SalesStatus == SalesStatus::Invoiced )

    {  

     //code

    }

}


9. Pre EventHandler

[PreHandlerFor(tableStr(PurchLineHistory), tableMethodStr(PurchLineHistory, update))]

public static void PurchLineHistory_Pre_update(XppPrePostArgs args)

{

    PurchLine _purchline;

    PurchLineHistory _purchLineHistory= args.getThis();

    _purchline=PurchLine::find(_purchLineHistory.PurchId,_purchLineHistory.LineNumber );

    _purchLineHistory.PriceDiscTableRefRecId =_purchline.PriceDiscTableRefRecId;

}

10. Form init Method COC

/// <summary>
/// LedgerJournalTransDaily Form Extension
/// </summary>
[ExtensionOf(formStr(LedgerJournalTransDaily))]
public final class LedgerJournalTransDaily_Extension
{
    /// <summary>
    /// Performs the basic initialization of the form.
    /// </summary>
    public void init()
    {
        FormRun formRun = this as FormRun;

        next init();
        
        //write your logic here
    }
}

SSRS report error message: The number of defined parameters is not equal to the number of cell definitions in the parameter panel.

Sometimes when we make any changes to the SSRS dataset and deploy the report. We face this kind of issue which is very common:


There are multiple solutions in order to solve this issue. Some are mentioned below:

Solution 1:
a) Create new design (Shouldn't duplicate it from existing design).
b) Copy all objects from old design and paste it in new design.
c) Save, Rebuild and deploy the report. It will work.

Solution 2:
a) Go to xml file of that report (Path : K:\AosService\PackagesLocalDirectory\ModelName\ModelName\AxReport)
b) Take a backup of that report for a safe side (optional)
c) Open that xml file in notepad (Hint: open notepad as administrator)
d) Remove "ReportParametersLayout" tag completely. See figure 2.1
e) Replace (in case of copy) or save the xml file and redeploy the report. It will work.

Figure 2.1:

Remove all the highlighted text coming inside ReportParametersLaout tag.








Happy Daxing :)