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
        {

        }
    }

No comments:

Post a Comment

Table browser URL in D365FO

Critical Thinking icon icon by Icons8