Dynamic queries in Dynamics 365 for Finance and Operations

Scenario

  1. Active checkbox to be created in ModelTable
  2. Create a lookup method in the form
  3. Write a dynamics query to filter the record.

CODE*


public void dynamicQueries()
    {
        Query			        query = new Query();
        QueryBuildDatasource    qbdsPOLMobileServiceTable, qbdsPOLModelTable;
        QueryBuildRange		    qbrModelId;

        qbdsPOLMobileServiceTable = query.addDataSource(tableNum(POLMobileServiceTable));

        qbdsPOLModelTable = qbdsPOLMobileServiceTable.addDataSource(tableNum(POLModelTable));
        qbdsPOLModelTable.relations(true); //Use Relations
        //qbdsPOLModelTable.addLink(fieldNum(POLMobileServiceTable, ModelId), fieldNum(POLModelTable, 
        ModelID));

        //Ranges
        qbdsPOLMobileServiceTable.addRange(fieldNum(POLMobileServiceTable, ModelID)).value('Apple');
        qbdsPOLMobileServiceTable.joinMode(JoinMode::InnerJoin);


        //How to run the query - Loop through the select statement
        QueryRun queryRun = new QueryRun(query);
        while (queryRun.next()) //Loop through the query
        {
            POLMobileServiceTable   serviceTable    = queryRun.get(tableNum(POLMobileServiceTable)); //Buffer 
            value from the query
            POLModelTable           modelTable      = queryRun.get(tableNum(POLModelTable)); //Buffer value 
            from the query

            info (strFmt("ServiceID: %1, IMEINumber: %2, ExpectedServiceAmount: %3, MinimumServiceAmount: %4", 
                    serviceTable.ServiceID,
                    serviceTable.IMEINumber, 
                    serviceTable.ExpectedServiceAmount,
                    modelTable.MinimumServiceAmount));
        }
    }