Wednesday, September 5, 2018
Dynamics 365 : How to use tempDB tables in queries
I had a requirement where a TempDB table is used in the Query along with other regular tables.
In order achieve this we need to use the method setRecord on QueryRun to assign a record from the TempDB table.
static void tempDBQuery(Args _args)
{
MyTmpIdList tmpIdList1, tmpIdList2;
Query query = new Query();
tmpIdList1.RefRecId = 1;
tmpIdList1.insert();
tmpIdList1.RefRecId = 2;
tmpIdList1.insert();
tmpIdList2.RefRecId = 1;
tmpIdList2.insert();
tmpIdList2.RefRecId = 2;
tmpIdList2.insert();
QueryBuildDataSource qbds1 = query.addDataSource(tableNum(MyTmpIdList));
QueryBuildDataSource qbds2 = qbds1.addDataSource(tableNum(MyTmpIdList));
qbds2.relations(false);
qbds2.joinMode(JoinMode::InnerJoin);
qbds2.addLink(fieldNum(MyTmpIdList, RefRecId), fieldNum(MyTmpIdList, RefRecId));
QueryRun queryRun = new QueryRun(query);
queryRun.setRecord(tmpIdList1, 1);
queryRun.setRecord(tmpIdList2, 2);
while(queryRun.next())
{
tmpIdList1 = queryRun.getNo(1);
tmpIdList2 = queryRun.getNo(2);
info(strFmt("%1 %2 %3 %4", tmpIdList1.RefRecId, tmpIdList1.RecId, tmpIdList2.RefRecId, tmpIdList2.RecId));
}
}
In the example the table MyTmpIdList is used twice in the query and hence the occurrence is also specified when calling the setRecord method. However in case there are multiple TempDB tables involved in the Query without any occurrence/repetition , you may just pass the TempDB table records for each TempDB tables in the query by calling the setRecord method.
Subscribe to:
Posts (Atom)