AX在插入一条纪录的时候会自动生成recId,每条纪录的recid是全局唯一的,如果知道了recid和tableid,关于如何找到纪录已经有很多的介绍,典型的做法是:
public Common findRecord(TableId _tableId, RecId _recId, Boolean _forUpdate = false) { Common common; DictTable dictTable; ; dictTable = new DictTable(_tableId); common = dictTable.makeRecord(); common.selectForUpdate(_forUpdate); select common where common.RecId == _recId; return common; }
还有一种做法是使用Query:
Query q = new Query; QueryBuildDatasource qbds; QueryRun qr; ; qbds = q.addDataSouce(tableid); qbds.addRange(tableid2name(tableid), RecId)).value(queryValue (yourrecid)); qr = new QueryRun(q); if (queryRun.next()) { common = qr.get(tableid); }
但是如果在我们不知道tableid的情况下如何来查找呢,似乎只能通过遍历所有的table的id了,遍历所有的table也有几种方法,我们依次来看。
第一种 使用Dictionary
Dictionary dictionary; TableId tableId; tableName tableName; common c; DictTable dt; ; dictionary = new Dictionary(); tableId = dictionary.tableNext(0); tableName = dictionary.tableName(tableId); while (tableId) { //info(strfmt("%1 - %2",int2str(tableId), tableName)); tableId = dictionary.tableNext(tableId); tableName = dictionary.tableName(tableId); dt = new DictTable(tableId); c=dt.makeRecord(); select c where c.RecId==53883; if(c) { info(strfmt("%1 - %2",int2str(tableId), tableName)); break; } }
第二种 使用SqlDictionary
SQLDictionary dictTable; common c; DictTable dt; ; while select * from dictTable where dictTable.fieldId == 0 { //info(strfmt("%1 - %2", dicttable.name ,dicttable.tabId)); dt = new DictTable(dictTable.tabId); c=dt.makeRecord(); select c where c.RecId==53883; if(c) { info(strfmt("%1 - %2", dicttable.name ,dicttable.tabId)); break; } }
此外还可以使用UtilIdElements来查找表:
UtilIdElements uie; common c; DictTable dt; ; while select uie where uie.recordType==utilelementtype::Table { info(strfmt("%1 --- %2",uie.name , uie.id)); dt = new DictTable(uie.id); c=dt.makeRecord(); select c where c.RecId==53883; if(c) { print uie.name; break; } }
但是这样运行AX就崩溃了,从打印出来的uie.name我们能看到重复的表名称,这是因为这个表可能在多个层上被修改过,查询时需要加入层的过滤,比如
while select uie where uie.recordType==utilelementtype::Table && uie.utilLevel==UtilEntryLevel::usr
但是这样的问题是你只能得到某一个层上的表对象,所以对这个问题处理不如方法一和方法二的查询办法了。还要说的是还有一个名为UtilElements的类,它和UtilIdElements的方法、属性基本上都上一样的,不同的地方是UtilElements没有id属性,只有一个source属性,这个source是个container对象,遍历了一下所有表,这个source都是空的,不知道是被设计来做什么的了。