Sunday, June 16, 2024

D365FO: Add financial dimension range in Query using X++

protected void createReportLines()
{
	Query                q;
	QueryRun             qr;
	QueryBuildDataSource qbds, qbdsInventTrans;
	RecId                dimAttrRecId = AgreementHeaderExt_RU::getAgreementDimensionAttribute();

	q = new Query();

	qbdsInventTrans = q.addDataSource(tableNum(InventTrans));

	findOrCreateRange_W(qbdsInventTrans, fieldNum(InventTrans, TableId), strFmt(issueReceiptValue,
																				qbdsInventTrans.name(),
																				enum2int(StatusIssue::Sold),
																				enum2int(StatusReceipt::Purchased)));
	findOrCreateRange_W(qbdsInventTrans, fieldNum(InventTrans, DateFinancial), queryRange(commReportJour.StartDate, commReportJour.EndDate));

	qbds = qbdsInventTrans.addDataSource(tableNum(InventTransOrigin));
	qbds.addLink(fieldNum(InventTrans, InventTransOrigin), fieldNum(InventTransOrigin, RecId));
	qbds.joinMode(JoinMode::InnerJoin);
	qbds.fetchMode(QueryFetchMode::One2One);

	qbds = qbds.addDataSource(tableNum(CustInvoiceTrans));
	qbds.addLink(fieldNum(InventTransOrigin, InventTransId), fieldNum(CustInvoiceTrans, InventTransId));
	qbds.addLink(fieldNum(InventTrans, InvoiceId), fieldNum(CustInvoiceTrans, InvoiceId), qbdsInventTrans.name());
	qbds.joinMode(JoinMode::InnerJoin);

	qbds = qbds.addDataSource(tableNum(CustInvoiceJour));
	qbds.relations(true);
	qbds.joinMode(JoinMode::InnerJoin);

	findOrCreateRange_W(qbds, fieldNum(CustInvoiceJour, InvoiceAccount), queryValue(commReportJour.PartnerCode));

	SysQuery::addDimensionAttributeRange(q,
	qbds.name(),
	fieldStr(CustInvoiceJour, DefaultDimension),
	DimensionComponent::DimensionAttribute,
	commReportJour.AgreementId,
	DimensionAttribute::find(dimAttrRecId).Name);

	qbds = qbds.addDataSource(tableNum(CustInvoiceJour_RU));
	qbds.relations(true);
	qbds.joinMode(JoinMode::ExistsJoin);

	findOrCreateRange_W(qbds, fieldNum(CustInvoiceJour_RU, InventProfileType_RU), con2Str([InventProfileType_RU::CommissionAgent,
																					   InventProfileType_RU::CommissionPrincipalAgent]));

	qbds = qbdsInventTrans.addDataSource(tableNum(InventDim));
	qbds.addLink(fieldNum(InventTrans, InventDimId), fieldNum(InventDim, InventDimId));
	qbds.joinMode(JoinMode::InnerJoin);
	qbds.fetchMode(QueryFetchMode::One2One);

	qr = new QueryRun(q);

	while (qr.next())
	{
		inventTrans         = qr.get(tableNum(InventTrans));
		inventTransOrigin   = qr.get(tableNum(InventTransOrigin));
		custInvoiceTrans    = qr.get(tableNum(CustInvoiceTrans));
		custInvoiceJour     = qr.get(tableNum(CustInvoiceJour));
		inventDim           = qr.get(tableNum(inventDim));

		this.processVendShipments();
	}
}

Get all menu items and its label metadata under a module D365F&O

using Microsoft.Dynamics.AX.Metadata.MetaModel;
using Microsoft.Dynamics.AX.Security.Management;
internal final class RunnableClass1
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        AxMenu Menu = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenu(menuStr(AccountsReceivable));

        SecurityRepository sr = new SecurityRepository();
        sr   = SysSecurity::GetSecurityRepository();

        var allPrivileges = sr.Privileges;
        var allPrivilegesEnumerator = allPrivileges.LoadAll().GetEnumerator();

        var   f = Menu.Elements.GetEnumerator();
        container conMenuItem ;
        int pos;
        while (f.MoveNext())
        {
            AxMenuElementSubMenu subMenu = f.Current;

            var   s= subMenu.Elements.GetEnumerator();
            while (s.MoveNext())
            {
                pos++;
                AxMenuElement menuElement = s.Current;

                if (Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemDisplay(menuElement.Name) ||
                    Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemOutput(menuElement.Name) ||
                    Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemAction(menuElement.Name))
                {
                    AxMenuElementMenuItem menuElementMenuItem = s.Current;
                    Label label = new Label(infolog.language());
                    switch (menuElementMenuItem.MenuItemType)
                    {
                       

                        case MenuItemType::Display:
                        if (Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemDisplay(menuElementMenuItem.MenuItemName))
                        {
                            AxMenuItemDisplay MenuItemDisplay = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemDisplay(menuElementMenuItem.MenuItemName);

                                info(strFmt("%1-%2-%3-%4-%5",Menu.Name, subMenu.Name,label.extractString(MenuItemDisplay.Label)  ,MenuItemDisplay.Name, MenuItemDisplay.Object));
                        }
                        break;
                        case  MenuItemType::Output:
                        if (Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemOutput(menuElementMenuItem.MenuItemName))
                        {
                            AxMenuItemOutput MenuItemOutput = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemOutput(menuElementMenuItem.MenuItemName);

                            //info(strFmt("%1-%2-%3",Menu.Name, subMenu.Name, menuElementMenuItem.MenuItemName));

                                info(strFmt("%1-%2-%3-%4-%5",Menu.Name, subMenu.Name, label.extractString(MenuItemOutput.Label) ,MenuItemOutput.Name, MenuItemOutput.Object));
                        }
                        break;
                        case  MenuItemType::Action:
                        if (Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemAction(menuElementMenuItem.MenuItemName))
                        {
                            AxMenuItemAction MenuItemAction = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenuItemAction(menuElementMenuItem.MenuItemName);

                            //info(strFmt("%1-%2-%3",Menu.Name, subMenu.Name, menuElementMenuItem.MenuItemName));
                                info(strFmt("%1-%2-%3-%4-%5",Menu.Name, subMenu.Name, label.extractString(MenuItemAction.Label) ,MenuItemAction.Name, MenuItemAction.Object));
                        }
                        break;
                    }

                    //info(strFmt("%1-%2-%3",Menu.Name, subMenu.Name, menuElement.Name));

                    conMenuItem += strFmt("%1",menuElement.Name);
                   // conIns(conMenuItem,pos, menuElement.Name);
                }
                else
                {
                    pos++;
                    info(strFmt("%1-%2-%3",Menu.Name, subMenu.Name, menuElement.Name));

                    conMenuItem += strFmt("%1",menuElement.Name);
                   // conIns(conMenuItem,pos, menuElement.Name);
                }

                
            }
        }

        //for (int i = 1 ; i <= conLen(conMenuItem) ; i++)
        //{
        //    while (allPrivilegesEnumerator.MoveNext())
        //    {
        //        var securityPrivilege = allPrivilegesEnumerator.Current;

        //        //while (securityPrivilege.MoveNext())
        //        //{

        //        //}
        //    }
        //   // print conPeek(c, i);
        //}

    }

}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8