Showing posts with label Dynamic query in D365. Show all posts
Showing posts with label Dynamic query in D365. Show all posts

Monday, July 8, 2019

Create Query dynamically in AOT through code:

Create Query dynamically in AOT through code:

static void CreateQueryInAOT(Args _args)
    {
        TreeNode                treeNodeObj;
        Query                   queryObj; // Extends TreeNode class.
        QueryBuildDataSource    qbds;
        QueryBuildRange         qbr;
        QueryRun                qr;
        CustTable               xrecCustTable;
        str                     queryName = "TestQuery";
        // Macro.
        #AOT
        // Delete the query from the AOT, if the query exists.
        treeNodeObj = TreeNode::findNode(#QueriesPath);
        treeNodeObj = treeNodeObj.AOTfindChild(queryName);
        if (treeNodeObj) { treeNodeObj.AOTdelete(); }
        // Add the query to the AOT.
        treeNodeObj = TreeNode::findNode(#QueriesPath);
        treeNodeObj.AOTadd(queryName);
        queryObj = treeNodeObj.AOTfindChild(queryName);
        // Further define the query.
        qbds  = queryObj.addDataSource(tablenum(CustTable));
        qbr   = qbds.addRange(fieldnum(CustTable, CustGroup));
        qbr.value("Inter");
        // Compile the query.
        queryObj.AOTcompile(1);
        queryObj.AOTsave();
        // Run the query.
        qr = new QueryRun("TestQuery");
        while ( qr.next() )
        {
            xrecCustTable = qr.GetNo(1); // 1 means first data source.
            Global::info(strFmt("%1 , %2",
                xrecCustTable.AccountNum, xrecCustTable.CustGroup));
        }