Iterating over any table - axapta

I just started new job where they use dynamics ax 2009. I am new to this technology.
Is there a way in x++ to iterate over any table?
I don't know where the data comes from, it's lenght nor field count.
What I mean by that is I need a function that would behave like this
void convert(Table anyTable)
{
int i=0;
int k=0;
;
for(i; i < anyTable.Lenght; i++)
{
for(k; k < anyTable[i].Count; k++)
{
//some xml processing
}
}
}
(By Table i mean some kind of parent of all tables). And that basically is my question - is there a parent of all tables or something of such sort that can help me achieve something like this?
I am sorry for formatting, im typing this from mobile device

The Common table is the base class for all tables. It does not contain any data. It is primarily used in X++ code to refer to any table in a polymorphic way. Please check Dictionary classes to solve your issue:
void convert(Common _common)
{
DictTable dictTable;
FieldId fieldId;
anytype value;
;
dictTable = new dictTable(_common.TableId);
if (dictTable)
{
while select _common
{
fieldId = dictTable.fieldNext(0);
while (fieldId)
{
value = _common.(fieldId);
//do processing
fieldId = dictTable.fieldNext(fieldId);
}
}
}
}

See this answer. It involves use of class Dictionary and DictTable for reflection.

Check out xml() method on Common class, it may help generate your XML representing the current record.

Related

Dynamics AX - Adding tables to DatabaseLog programatically in AX 2009

I'm looking for a way to enable logging changes for certain tables.
I have tried and tested adding tables to database log programatically, but with various success so far - sometimes it works sometimes it doesn't (mostly it does not) - it seems simply inserting rows into DatabaseLog table doesn't quite do the trick.
What I have tried:
Adding row with proper tableId, fieldId, logType and . Domain had been assigned as 'Admin', main company, empty field and subcompanies with the same result.
I have created class that handles inserts, main two functions are:
public static void InsertBase(STR tableName, domainId _domain='Admin')
{
//base logging for insert, delete, uptade on fieldid=0
DatabaseLog DBDict;
TableId _tableId;
DatabaseLogType _logType;
fieldId _fieldId =0;
List logTypes;
int i;
ListEnumerator enumerator;
;
_tableId= tableName2id(tableName);
logTypes = new List(Types::Enum);
logTypes.addEnd(DatabaseLogType::Insert);
logTypes.addEnd(DatabaseLogType::Update);
logTypes.addEnd(DatabaseLogType::Delete);
logTypes.addEnd(DatabaseLogType::EventInsert);
logTypes.addEnd(DatabaseLogType::EventUpdate);
logTypes.addEnd(DatabaseLogType::EventDelete);
enumerator = logTypes.getEnumerator();
while(enumerator.moveNext())
{
_logType = enumerator.current();
select * from dbdict where
dbdict.logTable==_tableId && dbdict.logField==_fieldId
&& dbdict.logType==_logType;
if(!dbDict) //that means it doesnt exist
{
dbdict.logTable=_tableId;
dbdict.logField=_fieldId;
dbdict.logType=_logType;
dbdict.domainId=_domain;
dbdict.insert();
}
}
info("Success");
}
and the method that lists every single field and adds as logType::Update
public static void init(str TableName, DomainId domain='Admin')
{
DatabaseLogType logtype;
int i;
container kk, ll;
DatabaseLog dblog;
tableid _tableId;
fieldid _fieldid;
;
logtype = DatabaseLogType::Update;
//holds a container of not yet added table fields to databaselog
kk = BLX_AddTableToDatabaseLog::buildFieldList(logtype,TableName);
for(i=1; i <= conlen(kk);i++)
{
ll = conpeek(kk,i);
_tableid = tableName2id(tableName);
_fieldid = conpeek(ll,1);
info(strfmt("%1 %2", conpeek(ll,1),conpeek(ll,2)));
dblog.logType=logType;
dblog.logTable = _tableId;
dblog.domainId = domain;
dblog.logField =_fieldid;
dblog.insert();
}
}
result:
What am I missing ?
#EDIT with some additional info
Does not work for SalesTable and SalesLine, WMSBillOfLading.
I couldn't add log for SalesTable and SalesLine by using wizard in administration panel, but my colleague somehow did (she has done exactly the same things as me). We also tried to add log to various other tables and we often found out that she could while I could not and vice versa (and sometimes none managed to do it like in case of WMSBillOfLading table).
The inconsistency of this mechanism is what drove me to write this code, which I hoped would solve all the problems.
After doing your setup changes you probably have to call
SysFlushDatabaseLogSetup::main();
in order to flush any caches.
This method is also called in the standard AX code in the form method SysDatabaseLogTableSetup\Methods\close and in the class method SysDatabaseLogWizard\doRun.

How to refresh Factbox

I have a form, when i click on my button.It adds to my table A (what my factbox shows)is it possible to refresh the factbox with X++ code? I can't figure out how to refresh my infopart or query which factbox uses.
For an infopart you can call an update of the data source of the infopart's form run:
void clicked()
{
PartList partList;
int i;
FormRun infoPartFormRun;
FormDataSource infoPartDataSource;
super();
partList = new PartList(element);
for (i = 1; i <= partList.partCount(); i++)
{
infoPartFormRun = partList.getPartById(i);
if (infoPartFormRun.name() == identifierStr(MyInfoPart))
{
infoPartDataSource = infoPartFormRun.dataSource();
if (infoPartDataSource)
{
infoPartDataSource.research();
}
}
}
}
I added the check for the infoPartDataSource because I first tested this with a cue group fact box, which does not have a data source (or at least I could not figure out how to get the data source of one of the cues in the cue group and since you asked for an infopart fact box, I did not investigate further).
Update: The issue seems to be popular at the moment, Martin DrĂ¡b also wrote in his blog about it: Refreshing form parts

How to identify advanced query or dynamic joins from query window?

In the query window that pops up, if a user right clicks and chooses "1:n" and selects a table, how can one detect and use that table? I have a good sample job and screenshots that should demonstrate what I'm trying to accomplish.
I wrote this sample job that dumps out the AOT query objects but not the dynamically joined table/range/value.
static void InventSumQuery(Args _args)
{
Query query = new Query(queryStr(InventDimPhys));
QueryRun qr = new QueryRun(query);
QueryBuildRange queryRange;
DictField dictField;
int i, n;
if(qr.prompt())
{
for (n=1; n<=query.dataSourceCount(); n++)
{
for (i=1; i<=query.dataSourceNo(n).rangeCount(); i++)
{
queryRange = query.dataSourceNo(n).range(i);
dictField = new dictField(query.dataSourceNo(n).table(), fieldName2id(query.dataSourceNo(n).table(), queryRange.AOTname()));
info(strFmt("%1.%2", tableId2name(dictField.tableid()), dictField.name()));
}
}
}
info("Done");
}
Of course I figure my own answer out. Query objects are static, and the query form actually just modifies the query when you make the change.
So you need to modify the code above to:
if(qr.prompt())
{
query = qr.query();
This gets the modified query. The advanced querying actually is just a function of the form itself that ultimately modifies the query.

Query to fetch table names from AX takes too long

I am using the following code in X++ to get table names:
client server public static container tableNames()
{
tableId tableId;
int tablecounter;
Dictionary dict = new Dictionary();
container tableNamesList;
for (tablecounter=1; tablecounter<=dict.tableCnt(); tablecounter++)
{
tableId = dict.tableCnt2Id(tablecounter);
tableNamesList = conIns(tableNamesList,1,dict.tableName(tableId));
}
return tableNamesList;
}
Business connector code :
tablesList = (AxaptaContainer)Global.ax.
CallStaticClassMethod("Code_Generator", "tableNames");
for (int i = 1; i <= tablesList.Count; i++)
{
tableName = tablesList.get_Item(i).ToString();
tables.Add(tableName);
}
The application hangs for 2 - 3 minutes while fetching data. What could be the cause? Any optimizations?
Rather than use ConIns, use +=, it will be faster
tableNamesList += dict.tableName(tableId);
ConIns has to work out where in the container to place the insert. += just adds it to the end
As mentioned before avoid conIns() when appending elements to a container because it makes a new copy of the container. Use += instead to append in place.
Also, you may want to check for permissions and leave out temporary tables, table maps, and other special cases. Standard Ax has a method to build a table name lookup form that takes these things into account. Check the method Global::pickTable() for details.
You could avoid some calls through the business connector as well and build the entire list in Ax in a similar way and return that in a single function call.
If you are using Dynamics Ax 2012, you could skip the treeNode stuff and use the SysModelElement table to fetch the data and return it immediately as a .Net Array to easy up things on the other side.
public static System.Collections.ArrayList FetchTableNames_ModelElementTables()
{
SysModelElement element;
SysModelElementType elementType;
System.Collections.ArrayList tableNames = new System.Collections.ArrayList();
;
// The SysModelElementType table contains the element types
// and we need the recId for the next selection
select firstonly RecId
from elementType
where elementType.Name == 'Table';
// With the recId of the table element type,
// select all of the elements with that type (hence, select all of the tables)
while select Name
from element
where element.ElementType == elementType.RecId
{
tableNames.Add(element.Name);
}
return tableNames;
}
}
Alright, I have tried a lot of things and in the end, I decided to create a table consisting of all table names. This table will have a Job populating it. I am fetching records from this table.

Looping through keys in ASP.NET cache object

Caching in ASP.NET looks like it uses some kind of associative array:
// Insert some data into the cache:
Cache.Insert("TestCache", someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");
// But, can be done associatively ...
someValue = Cache["TestCache"];
// Also, null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];
As you can see, performing a null check on the cache object is very useful.
But I would like to implement a cache clear function that can clear cache values
where I don't know the whole key name. As there seems to be an associative
array here, it should be possible (?)
Can anyone help me work out a way of looping through the stored cache keys and
performing simple logic on them? Here's what I'm after:
static void DeleteMatchingCacheKey(string keyName) {
// This foreach implementation doesn't work by the way ...
foreach(Cache as c) {
if(c.Key.Contains(keyName)) {
Cache.Remove(c);
}
}
}
Don't use a foreach loop when removing items from any collection type- the foreach loop relies on using an enumerator which will NOT allow you to remove items from the collection (the enumerator will throw an exception if the collection it is iterating over has items added or removed from it).
Use a simple while to loop over the cache keys instead:
int i = 0;
while (i < Cache.Keys.Length){
if (Cache.Keys(i).Contains(keyName){
Cache.Remove(Cache.Keys(i))
}
else{
i ++;
}
}
An other way to do it in .net core:
var keys = _cache.Get<List<string>>(keyName);
foreach (var key in keys)
{
_cache.Remove(key);
}

Resources