Showing posts with label event handler. Show all posts
Showing posts with label event handler. Show all posts

Wednesday, June 9, 2021

How to add reference fields in forms that don't have a data source

 

Create a temp table with reference fields you wanted 


Add the Temp table as a data source in the required from without any join source


To initialize values when opening form

    [FormDataSourceEventHandler(formDataSourceStr(PdsResetVendorBatchDetails, PdsVendBatchInfo), FormDataSourceEventType::Initialized)]
    public static void PdsVendBatchInfo_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
    {
            FormRun formRun = sender.formRun() as FormRun;

            switch (formRun.args().dataset())
            {
                case tableNum(InventBatch) :

                    InventBatch  inventBatch = formRun.args().record() as InventBatch;

                    PdsVendBatchInfoTMP PdsVendBatchInfo1 = sender.cursor();


                    FormReferenceControl Manufact = formRun.design().controlName(formControlStr(PdsResetVendorBatchDetails, PdsVendBatchInfo_Manufact)) as FormReferenceControl;
                    FormReferenceControl ManufactSite = formRun.design().controlName(formControlStr(PdsResetVendorBatchDetails, PdsVendBatchInfo_ManufactSite)) as FormReferenceControl;
                
                    PdsVendBatchInfo1.Manufact = inventBatch.Manufact;
                    PdsVendBatchInfo1.ManufactSite = inventBatch.ManufactSite;

                    PdsVendBatchInfo1.doInsert();
                
                    sender.setRecord(PdsVendBatchInfo1);

                    Manufact.value(inventBatch.Manufact);
                    ManufactSite.value(inventBatch.ManufactSite);
                    break;
            }
    }

 

Lookup

public static void dirPartyLocationLookup(FormReferenceGroupControl _ctrl, DirPartyRecId _party)
    {
        container               conLocationRoles;
        LogisticsLocationRole   logisticsLocationRole;
        QueryBuildDataSource    qbdsDirPartyLocation, qbdsDirPartyLocationRole;

        Query   query = new Query();
        SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tablenum(DirPartyLocation), _ctrl);

        while select RecId from LogisticsLocationRole
            where LogisticsLocationRole.IsManufactSite
        {
            conLocationRoles += LogisticsLocationRole.RecId;
        }

        sysTableLookup.addLookupfield(fieldNum(DirPartyLocation, Location));
        sysTableLookup.addLookupfield(fieldNum(DirPartyLocation, Party));
        
        qbdsDirPartyLocation = query.addDataSource(tablenum(DirPartyLocation));
        
        qbdsDirPartyLocationRole = qbdsDirPartyLocation.addDataSource(tablenum(DirPartyLocationRole));
        qbdsDirPartyLocationRole.joinMode(JoinMode::ExistsJoin);
        qbdsDirPartyLocationRole.relations(true);

        qbdsDirPartyLocationRole.addRange(fieldNum(DirPartyLocationRole, LocationRole)).value(con2Str(conLocationRoles));
       
        if(_party)
        {
            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Party)).value(queryValue(_party));

            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Location)).value(SysQuery::valueNot(0));
        }
        else
        {
            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Party)).value(SysQuery::valueEmptyString());
        }

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

    public static void DirPartyIdLookup(FormControl _ctrl)
    {
        Query   query = new Query();

        SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tablenum(DirPartyTable), _ctrl);

        sysTableLookup.addLookupfield(fieldNum(DirPartyTable, PartyNumber));
        sysTableLookup.addLookupfield(fieldNum(DirPartyTable, Name));

        query.addDataSource(tablenum(DirPartyTable));
        
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

Monday, April 12, 2021

Listpage interaction class event handlers in D365fo

/// <summary>
/// This class is used to handle the event on SalesTableListPage form
/// </summary>
final class SM_Sales_SalesTableListPageInteractionEventHandler
{
    /// <summary>
    /// Sets the visibility property on the SM_SalesCalculateCumulativeGrossWeight button in the action pane.
    /// </summary>
    /// <param name="args"></param>
    [PostHandlerFor(classStr(SalesTableListPageInteraction), methodStr(SalesTableListPageInteraction, setButtonVisibility))]
    public static void SalesTableListPageInteraction_Post_setButtonVisibility(XppPrePostArgs args)
    {
        //CustParameters custParameters = CustParameters::find();

        //SalesTableListPageInteraction salesTableListPageInteraction = args.getThis();

        //salesTableListPageInteraction.listPage().actionPaneControlVisible(formControlStr(SalesTableListPage, SM_SalesCalculateCumulativeGrossWeight), custParameters.SM_ActivateSOGrossWeightCalculation);

    }

}
class LTSalesTableEventHandler
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SalesTable), FormDataSourceEventType::Activated)]
    public static void SalesTable_OnActivated(FormDataSource sender, FormDataSourceEventArgs e)
    {
        SalesTable          salesTable = sender.cursor();
        FormRun             element = sender.formRun();
        FormControl         sendPOToDeal = element.design(0).controlName("SendSOToDeal");
        FormControl         resendPOToDeal = element.design(0).controlName("ResendSOToDeal");
        
        if (salesTable.LTSOSent == NoYes::No && salesTable.SalesType == SalesType::Sales && salesTable.SalesStatus == SalesStatus::Backorder)
        {
            sendPOToDeal.enabled(true);
        }
        else
        {
            sendPOToDeal.enabled(false);
        }

        if (salesTable.LTSOSent == NoYes::Yes && salesTable.SalesType == SalesType::Sales && salesTable.SalesStatus == SalesStatus::Backorder)
        {
            resendPOToDeal.enabled(true);
        }
        else
        {
            resendPOToDeal.enabled(false);
        }

        if (salesTable.SalesType != SalesType::Sales)
        {
            sendPOToDeal.enabled(false);
            resendPOToDeal.enabled(false);
        }

    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="args"></param>
    [PostHandlerFor(classStr(SalesTableListPageInteraction), methodStr(SalesTableListPageInteraction, selectionChanged))]
    public static void SalesTableListPageInteraction_Post_selectionChanged(XppPrePostArgs args)
    {
        SalesTableListPageInteraction salesTableInteraction = args.getThis();
        SalesTable  salestable  = salesTableInteraction.listPage().activeRecord(tableStr(SalesTable));

        if (salesTable.LTSOSent == NoYes::No && salesTable.SalesType == SalesType::Sales && salesTable.SalesStatus == SalesStatus::Backorder)
        {
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, SendSOToDeal), true);
        }
        else
        {
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, SendSOToDeal), false);
        }

        if (salesTable.LTSOSent == NoYes::Yes && salesTable.SalesType == SalesType::Sales && salesTable.SalesStatus == SalesStatus::Backorder)
        {
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, ResendSOToDeal),true);
        }
        else
        {
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, ResendSOToDeal), false);
        }

        if (salesTable.SalesType != SalesType::Sales)
        {
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, SendSOToDeal), false);
            salesTableInteraction.listPage().actionPaneControlEnabled(formControlStr(SalesTable, ResendSOToDeal), false);
        }

    }

}

/// <summary>
/// This class is used to handle the event on SalesTableListPageInteraction form
/// </summary>
[Extensionof(classStr(SalesTableListPageInteraction))]
final class SM_Sales_SalesTableListPageInteraction_Extension
{
    protected void setButtonVisibility()
    {
        next setButtonVisibility();

        CustParameters custParameters = CustParameters::find();
        
        this.listPage().actionPaneControlVisible(formControlStr(SalesTableListPage, SM_SalesCalculateCumulativeGrossWeight), custParameters.SM_ActivateSOGrossWeightCalculation);
    }

}

Form Init, Initialized, writing event handlers in D365fo

/// <summary>
/// This class is used to handle the event on SalesTable form
/// </summary>
public static class SM_Sales_SalesTableForm_Extension
{    
    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SM_SalesTable), FormDataSourceEventType::Writing)]
    public static void SM_SalesTable_OnWriting(FormDataSource sender, FormDataSourceEventArgs e)
    {   
        FormDataSource      fdsSalesTable = sender.formRun().dataSource("SalesTable");
        SalesTable           salesTable = fdsSalesTable.cursor();

        FormDataSource      fdsSMSalesTable = sender.formRun().dataSource("SM_SalesTable");
        SM_SalesTable           smSalesTable = fdsSMSalesTable.cursor();       

        if(!smSalesTable.SalesTable)
        {
            smSalesTable.SalesTable = salesTable.RecId;
        }
        
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormDataSourceEventHandler(formDataSourceStr(SalesTable, SM_SalesLine), FormDataSourceEventType::Writing)]
    public static void SM_SalesLine_OnWriting(FormDataSource sender, FormDataSourceEventArgs e)
    {
        FormDataSource      fdsSalesLine = sender.formRun().dataSource("SalesLine");
        SalesLine           salesLine = fdsSalesLine.cursor();

        FormDataSource      fdsSMSalesLine = sender.formRun().dataSource("SM_SalesLine");
        SM_SalesLine          smSalesLine = fdsSMSalesLine.cursor();

        if(!smSalesLine.SalesLine)
        {
            smSalesLine.SalesLine = salesLine.RecId;
        }
        
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormEventHandler(formStr(SalesTable), FormEventType::Initialized)]
    public static void SalesTable_OnInitialized(xFormRun sender, FormEventArgs e)
    {
        CustParameters custParameters   = CustParameters::find();
        FormDataSource SM_SalesTable_ds = sender.dataSource(formDataSourceStr(SalesTable, SM_SalesTable));
        FormDataSource SM_SalesLine_ds  = sender.dataSource(formDataSourceStr(SalesTable, SM_SalesLine));

        SM_SalesTable_ds.object(fieldNum(SM_SalesTable, CumulativeGrossWeight)).visible(custParameters.SM_ActivateSOGrossWeightCalculation);
        SM_SalesLine_ds.object(fieldNum(SM_SalesLine, GrossWeight)).visible(custParameters.SM_ActivateSOGrossWeightCalculation);
        sender.control(sender.controlId(formControlStr(SalesTable, SM_SalesCalculateCumulativeGrossWeight))).visible(custParameters.SM_ActivateSOGrossWeightCalculation);

    }

}

class LogisticsPostalAddressGrid_Eventhandler
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="args"></param>
    [PostHandlerFor(formStr(LogisticsPostalAddressGrid), formMethodStr(LogisticsPostalAddressGrid, init))]
    public static void LogisticsPostalAddressGrid_Post_init(XppPrePostArgs args)
    {
        FormRun             element       = Args.getThis(); //form element
        FormControl         myNewButton   = element.design(0).controlName("MapButton");

        myNewButton.visible(false);
    }

}

Form control Modified event handler in D365fo

 [FormControlEventHandler(formControlStr(EcoResProductCreate, SAZBaseItemIdentification_ItemId), FormControlEventType::Modified)]
    public static void SAZBaseItemIdentification_ItemId_OnModified(FormControl sender, FormControlEventArgs e)
    {
        formRun callerForm = sender.formRun();

        FormStringControl senderStringControl = sender as FormStringControl;

        FormStringControl ProductLifecycleStateIdStringCtrl = callerForm.design().controlName(formControlStr(EcoResProductCreate, ProductLifecycleStateId)) as FormStringControl;

        InventTable inventTable = InventTable::find(senderStringControl.valueStr());

        if(inventTable)
        {
            if (callerForm && formHasMethod(callerForm, identifierstr(templateRecords2Controls)))
            {
                //setting data 2 Ecorsproduct controls
                callerForm.templateRecords2Controls(BaseItemTemplateHelper::construct().dataSourceListWithRecordMap(inventTable.ItemId));
            }
        }

        senderStringControl.text(inventTable.RecId != 0 ? inventTable.ItemId : strMin());

    }

Form control lookup event handler in D365fo

 [FormControlEventHandler(formControlStr(EcoResProductCreate, SAZBaseItemIdentification_ItemId), FormControlEventType::Lookup)]
    public static void SAZBaseItemIdentification_ItemId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        SysTableLookup      sysTableLookup  = SysTableLookup::newParameters(tableNum(InventTable), sender);
        Query               query           = new Query();

        QueryBuildDataSource qbdsInventTable = query.addDataSource(tableNum(InventTable));
        qbdsInventTable = qbdsInventTable.addDataSource(tableNum(EcoResProductLifecycleState)); 
        qbdsInventTable.joinMode(JoinMode::InnerJoin); 
        qbdsInventTable.relations(true);
        
        qbdsInventTable.addRange(fieldNum(EcoResProductLifecycleState, SAZIsCopyItemSourceStatus)).value(enum2Str(NoYes::Yes));
        
        sysTableLookup.addLookupfield(fieldNum(InventTable, ItemId));
        sysTableLookup.addLookupMethod(tableMethodStr(InventTable, itemName));

        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();

        FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
        ce.CancelSuperCall();
    }

Table browser URL in D365FO

Critical Thinking icon icon by Icons8