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
    }
}

No comments:

Post a Comment