Showing posts with label Query. Show all posts
Showing posts with label Query. Show all posts

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();
	}
}

Monday, January 23, 2023

update_recordset using queryrun class in D365FO

//intialize the query
Query                  salesActivityLineQuery = new Query();
QueryBuildDataSource    SalesActivityLineDS = salesActivityLineQuery.addDataSource(tableNum(SalesActivityLine));
SalesActivityLineDS.addRange(fieldNum(SalesActivityLine_DIS, RecId)).value(con2Str(conSalesActivityLineRecId));

//Initialize Map and specify the field and field value to be updated
Map fieldSetMap1 = new Map(Types::String, Types::String);
fieldSetMap1.insert(fieldStr(SalesActivityLine_DIS, Status), any2Str(ActivityLineStatus_DIS::Rejected));

//Update
Query::update_recordset(fieldSetMap1, salesActivityLineQuery);

Saturday, April 24, 2021

Query with join with multiple data sources in AX (Inner join, not exists join)

    public void initializeQueryDS()
    {
        query = new Query();
        QueryBuildDataSource qbds;
        QueryBuildDataSource qbdsSessionLine;

        switch(routing)
        {
            case Routing::Sales:
                qbds = query.addDataSource(tableNum(SalesLine));

                qbdsSessionLine = qbds.addDataSource(tableNum(WorkbenchSessionLine));
                qbdsSessionLine.joinMode(JoinMode::NoExistsJoin);
                qbdsSessionLine.relations(true);
                qbdsSessionLine.fetchMode(QueryFetchMode::One2One);

                TblNum = tableNum(SalesLine);
                break;
            case Routing::Return:
                qbds = query.addDataSource(tableNum(SalesLine));
                TblNum = tableNum(SalesLine);
                break;
        }
    }

    public void initializeQueryRange()
    {
        if(inventLocationId != strMin())
        {  
            QueryBuildDataSource qbdsSalesLine,qbdsInventDim;

            qbdsSalesLine = query.dataSourceTable(tableNum(SalesLine));
            qbdsInventDim = qbdsSalesLine.addDataSource(tableNum(InventDim));
            qbdsInventDim.joinMode(JoinMode::InnerJoin);
            qbdsInventDim.relations(true);
            qbdsInventDim.fetchMode(QueryFetchMode::One2One);
            qbdsInventDim.addRange(fieldNum(InventDim, InventLocationId)).value(inventLocationId);
        }

        if(shipDate != dateNull())
        {
            QueryBuildDataSource qbds;

            qbds = query.dataSourceTable(tableNum(SalesLine));
            qbds.addRange(fieldNum(SalesLine, ShippingDateConfirmed)).value(SysQuery::value(shipDate));
        }

        if(dlvModeFrom.elements())
        {
            container   conMOD;
            Enumerator  enumerator  =   dlvModeFrom.getEnumerator();

            while (enumerator.moveNext())
            {
                conMOD += enumerator.current();
            }

            QueryBuildDataSource qbds;
            qbds = query.dataSourceTable(tableNum(SalesLine));

            qbds.addRange(fieldNum(SalesLine, DlvMode)).value(con2Str(conMOD));
        }

    }

    public WorkbenchSessionLine initFromCommon(Common _callerTable)
    {
        WorkbenchSessionLine line;

        switch (_callerTable.TableId)
        {
            case tableNum(SalesLine):
                line.RefRecId    = _callerTable.(fieldNum(SalesLine, RecId));
                line.RefTableId    = _callerTable.TableId;

                line.InventTransId    = _callerTable.(fieldNum(SalesLine, InventTransId));

                if(routing == Routing::Sales)
                {
                    line.ReferenceType = RefType::SalesOrder;
                }
                else if(routing == Routing::Return)
                {
                    line.ReferenceType = RefType::ReturnOrder;
                }
                break;

            default:
                break;
        }

        return line;
    }

    public void createWorkbenchSession(WorkbenchSessionContract _contract)
    {
        WorkbenchSessionTable header;
        WorkbenchSessionLine line;

        RecordInsertList lineRecordList = new RecordInsertList(tableNum(WorkbenchSessionLine), true, true, true, false, true, line);

        contract = _contract;
       
        this.initializeParameters();
        this.initializeQueryDS();
        this.initializeQueryRange();

        try
        {
            ttsbegin;

            NumberSeq numberSeq = NumberSeq::newGetNum(TMSParameters::numRefInternalSessionID());

            header.initValue();

            header.DynamicsInternalSessionID = numberSeq.num();

            numberSeq.used();

            header.SessionStatus = SessionStatus::Open;

            header.LE_ID = curExt();

            if (routing == Routing::Sales || routing == Routing::Return)
            {
                header.CombineOrders = true;
            }
           
            header.insert();

            queryRun = new QueryRun(query);

            while(queryRun.next())
            {
                Common common = queryRun.get(TblNum);

                line = this.initFromCommon(common);

                line.WorkbenchSessionTable = header.RecId;

                lineRecordList.add(line);
            }

            lineRecordList.insertDatabase();

            ttscommit;
            
            this.openWorkbenchSessionForm(header);
        }
        catch
        {

        }
    }

Sunday, April 11, 2021

Run Query run

QueryRun queryRun = new QueryRun(this.getQueryOrderHoldRules());

        while (queryRun.next())
        {
            MCRHoldCodeTable mcrHoldCodeTable = queryRun.get(tableNum(MCRHoldCodeTable));

            this.checkExistingOrdeHolds(mcrHoldCodeTable);

        }

Query run with dynamic ranges

public Query getQueryOrderHoldRules()
    {
        Query                   query;
        QueryBuildDataSource    qbdsMCRHoldCodeTable;

        query = new Query();

        qbdsMCRHoldCodeTable = query.addDataSource(tableNum(MCRHoldCodeTable));

        switch (documentStatus)
        {
            case CredManDocumentStatusCheck::Confirmation:
                qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZConfirmation)).value(SysQuery::value(NoYes::Yes));
                break;
            case CredManDocumentStatusCheck::PickingList:
                qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZPickingList)).value(SysQuery::value(NoYes::Yes));
                break;
            case CredManDocumentStatusCheck::PackingSlip:
                qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZPackingSlip)).value(SysQuery::value(NoYes::Yes));
                break;

            case CredManDocumentStatusCheck::WHSShipment:
                qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZWHSShipment)).value(SysQuery::value(NoYes::Yes));
                break;

            case CredManDocumentStatusCheck::Invoice:
                qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZInvoice)).value(SysQuery::value(NoYes::Yes));
                break;

            default:
                break;
        }

        qbdsMCRHoldCodeTable.addRange(fieldNum(MCRHoldCodeTable, SAZHoldCodeRule)).value(SysQuery::valueNot(SAZHoldCodeRule::None));

        return query;

    }

Table browser URL in D365FO

Critical Thinking icon icon by Icons8