Monday, May 23, 2022

How to add On hand list in custom form using x++

Today we see how to add On hand list in custom form using x++ in D365FO. I got a requirement on adding  "On hand list" form to custom form. If we add this form directly system will not open this form because while opening this form they have written few logics for filtering the data based on item.


So we need add our custom table in those methods. In the below example I am using site and warehouse forms. Just check the code. 

1st we need to write the code in "InventDim" table.

[ExtensionOf(tableStr(InventDim))]
final static class InventDim_Extension
{
    static public void formQueryAddDynalinkByQuery(
                Query           _query,
                Args            _args,
                boolean         _linkInventDim)
    {
        QueryBuildDatasource 	inventDimDS, siteDs, inventLocationDS;
        InventLocation      	inventLocation;
        InventSite          	inventSite;

        switch (_args.dataset())
        {
            case tableNum(InventLocation):
                inventLocation      = _args.record();
                inventDimDS         = _query.dataSourceTable(tableNum(InventDim));
                inventLocationDS    = inventDimDS.addDataSource(tableNum(InventLocation));

                inventLocationDS.addLink(fieldNum(InventDim, InventLocationId),
                                fieldNum(InventLocation, InventLocationId));
                inventLocationDS.joinMode(JoinMode::ExistsJoin);
                inventLocationDS.addRange(fieldNum(InventLocation, InventLocationId)).value(
                                                inventLocation.InventLocationId);

                inventDimDS.addSortField(fieldNum(InventDim, InventLocationId));
                break;

            case tableNum(InventSite):
                inventSite  = _args.record();
                inventDimDS = _query.dataSourceTable(tableNum(InventDim));
                siteDs      = inventDimDS.addDataSource(tableNum(InventSite));

                siteDs.addLink(fieldNum(InventDim, InventSiteId),fieldNum(InventSite, SiteId));
                siteDs.joinMode(JoinMode::ExistsJoin);
                siteDs.addRange(fieldNum(InventSite, SiteId)).value(inventSite.SiteId);

                inventDimDS.addSortField(fieldNum(InventDim, InventSiteId));
                break;
        }

        next formQueryAddDynalinkByQuery(_query,_args,_linkInventDim);
    }
}

2nd we need to write the code in "InventOnHandItem" form.

[ExtensionOf(formstr(InventOnHandItem))]
final class PIDInventOnHandItemForm_Extension
{
    public void init()
    {
        next init();

        if (this.args().record() && this.args().dataset() == tableNum(InventLocation))
        {
            InventSum_DS.autoSearch(true);
            isFormOpenedWithDynalink = true;
        }

        if (this.args().record() && this.args().dataset() == tableNum(InventSite))
        {
            InventSum_DS.autoSearch(true);
            isFormOpenedWithDynalink = true;
        }
    }
}


Keep Daxing!!


No comments:

Post a Comment