Dec 2, 2013

List in AX

List is the structure which contain values of any same type, where we can access sequentially.

static void testList(Args _args)
{
List li = new List(Types::Integer);
ListEnumerator enumerator;
ListIterator lItr;
;
// Add some elements to the list
li.addStart(3);
li.addEnd(1);
li.addEnd(2);

info(strfmt("%1", li.definitionString())); // returns the "Type" of the list
info(strfmt("%1", li.toString())); // output --> <3,1,2>

enumerator = li.getEnumerator();
while(enumerator.moveNext())
{
info(Strfmt("Enumerator %1 ",enumerator.current()));
}

lItr = new ListIterator (li);
while(lItr.more()) // checks whether there are more elements in the list
{
info(strFmt("Iterator %1",lItr.value()));
lItr.next(); // Skips to the next element
}

}

Map In AX

The "Map" allows to associate one value (the key) with another value. Both the key and value can be any valid type.

static void testMap(Args _args)
{
Map m = new Map(Types::STRING, Types::INTEGER);
MapIterator mi;
MapEnumerator me = m.getEnumerator();


int i,test;
str keyId = "abc";
;

m.insert("abc", 37);
m.insert("def", 102);

i = m.lookup("abc");
test = m.exists(keyId)? m.lookup(keyId): 0;
m.insert(keyid, test + 1); // updation

if (m.exists("abc"))
info(strFmt("%1",m.lookup("abc")));


mi = new MapIterator(m);
while (mi.more())
{
info(strFmt("%1 Iterator", mi.key()));
info(strFmt("%1", mi.value()));

mi.next();
}


while (me.moveNext())
{
info(strFmt("%1 Enumertor",me.currentKey()));
info(strFmt("%1",me.currentValue()));
}

}

Set In AX

Set contains values of the same type, where value is unique and always sorted on a value

static void testSet(Args _args)
{
Set set = new Set(Types::Integer);
SetIterator sItr;
SetEnumerator sEnum ;

;
set.add(100);
set.add(101);
set.add(102);


info(strFmt("%1",set.toString()));
info(strFmt("%1",set.elements())); //No of elements
info(strFmt("%1",set.in(100))); //To see if a value already is added, use the in method:

set.remove(100);

info(strFmt("%1",set.elements())); //No of elements

// Getting values by using SetEnumerator

sEnum = set.getEnumerator();

while (sEnum.moveNext())
{
info(strFmt("%1",sEnum.current()));
}

// Getting values by using SetIterator

sItr = new SetIterator(set); // initializing set to setIterator
while(sItr.more())
{

info(strFmt("%1",sItr.value()));
sItr.next();
}


}

QueryRangeFilter in AX

static void QueryRangeFilter(Args _args)
{
Query query;
QueryBuildDataSource datasource;
QueryBuildRange range;
QueryFilter filter;
QueryRun queryRun;
int countAfterChange = 0, countBeforeChange = 0;

query = new Query();
datasource = query.addDataSource(tableNum(CustTable));
datasource = datasource.addDataSource(tableNum(SalesTable));
datasource.joinMode(JoinMode::InnerJoin);
datasource.relations(true);

datasource.addLink(fieldNum(CustTable, AccountNum),
fieldNum(SalesTable, CustAccount));

filter = query.addQueryFilter(datasource,
fieldStr(SalesTable, CurrencyCode));

filter.value(SysQuery::value('USD'));

//range = datasource.addRange(fieldNum(SalesTable, CurrencyCode));
//range.value(SysQuery::value('EUR'));

queryRun = new QueryRun(query);
while (queryRun.next())
{
countBeforeChange ++;
if (queryRun.changed(tableNum(CustTable)))
countAfterChange ++;
}

info(strFmt("CountAfterChange : %1", countAfterChange ));
info(strFmt("CountBeforeChange : %1", countBeforeChange ));
}