Thursday, May 21, 2009

What are the objects Dict* and How can we use them?

In some cases we need to write some codes in Dict objects. If we look under AOT\System Documentation\Classes node, here are the objects we will discuss in this post.

Dict objects are generally used for creating a object of related type (for DictClass e.g. CustBalanceList class), getting information about that object (for DictClass e.g. is class defined Abstract, Final or is Runnable {has main method} ) etc.

DictClass

Object can be constructed by “new” word (Params: ClassId). Mostly and effectively used for creating a child class (if we know classId of it).

Example usage in AX Classes\VenOutPaymRecord\newVendOutPaymRecord
Example code

//A child class will be created by using dictclass

DictClass dictClass;
VendOutPaymRecord vendOutPaymRecord; // parent class is defined
;

if (! _vendPaymModeSpec.classId)
{ // Payment mode specification has the child class id
return null;
}

if (! SysDictClass::isSuperclass(_vendPaymModeSpec.classId, classNum(VendOutPaymRecord)))
{ //Be sure if child class is really child
return null;
}

dictClass = new DictClass(_vendPaymModeSpec.classId); //Initialize dictclass object by child class id
vendOutPaymRecord = dictClass.makeObject(); // Create real child class by calling makeObject method

if (! vendOutPaymRecord) // Check if class was made or not
{
return null;
}

DictConfigurationKey

Object can be constructed by “new” word (Params: ConfigurationKeyId). Mostly used for checking if configuration key is enabled or not

Example usage in AX Classes\PurchLineType\interCompanyMirror
Example code

//Check if SalesDeliveryDateControl configuration key is enabled

if (new DictConfigurationKey(configurationkeynum(SalesDeliveryDateControl)).enabled())

DictEnum

Object can be constructed by “new” word (Params: EnumId). Mostly used for returning all of the elements of any enum object and accessing name of them.

Example usage in AX Classes\InventItemType\valueCanBeProduced
Example code

//Check if SalesDeliveryDateControl configuration key is enabled

DictEnum dictEnum;
Counter i;
InventItemType inventItemType;
str itemTypeTxt;
;

dictEnum = new DictEnum(enumnum(ItemType)); // ItemType is a variable that value determined at outer of method earlier
for (i=0;i// This block will loop until the end element of enum
{
inventItemType = InventItemType::construct(i);
if (inventItemType.canBeProduced())
{
itemTypeTxt += itemTypeTxt ? ',' : '';
itemTypeTxt += queryValue(dictEnum.value2Name(i)); // Here, enum name is extracting from enum value
}
}

return itemTypeTxt ? itemTypeTxt : SysQuery::valueEmptyString();

DictField

Object can be constructed by “new” word (Params: TableId, FieldId). Mostly used for finding basetype or label etc of any table field.

Example usage in AX Classes\SysApplCheck\checkFieldDefaultReport
Example code

// This example will show some functionalities of DictField class

#MacroLib.DictField // will be used for flag testing
DictField dictField = new DictField(Tablenum(ContactPerson), FieldNum(ContactPerson,Name));
;
dictField.enumId(); // If that field was the type of enum, that method would return enumid (now is zero)
dictField.arraySize(); // In default array size is 1 (for EDT Dimension , array size bigger than 1)
if (bitTest(dictField.flags(),#DBF_CHANGE))
info('Allow edit property of the field is true');
dictField.relationObject(); // can be used for determining main table of EDT of the field. That expression will return dictRelation object
dictField.label(); // label of the field
dictField.stringLen(); // if field is the typeof string, that will return length of string (nonzero) value

DictFieldGroup

Object can be constructed by “new” word (Params: TableId, FieldGroupName). Mostly used for determining the fields in the fieldgroup of a table.

Example usage in AX

Classes\SysReportWizard\fieldFillContainer
Example code

// This example will show some functionalities of DictFieldGroup class

dictFieldGroup = new DictFieldGroup(TableNum(CustTable), ‘Address’); // Will Process Address fieldgroup of CustTable
fieldGroupName = dictFieldGroup.name(); // will return the string ‘Address’
fieldGroupElement = connull();
lastFieldElement = 0;

for (j=1; j <= dictFieldGroup.numberOfFields(); j++) //Determine the # of fileds in group
{
methodName = dictFieldGroup.methodName(dictFieldGroup.field(j)); //If the element in group is display or edit method ‘methodname’ variable will be the name of the method
if (methodName) //If the element is method then do something…
{

}
else // otherwise this is a table field
{
sysDictField = new SysDictField(dictTable.id(), dictFieldGroup.field(j)); // get dictfield and do something…
for(k = 1; k <= sysDictField.arraySize(); k++)
{

}
}
}

DictIndex

Object can be constructed by “new” word (Params: TableId, IndexId). Mostly used for finding the fields of the index or checking if it allow duplicates or not.

Example usage in AX

Classes\SysDictField\isUnique

Example code

// This example will show some functionalities of DictIndex class

DictTable dictTable;
DictIndex dictIndex;
int indexId;
int i;
fieldId fieldId;

if (this.mandatory())
{
dictTable = new DictTable(this.tableid());
fieldId = fieldExt2Id(this.id());
if (dictTable.primaryKeyField() == fieldId)
return true;

for (indexId = dictTable.indexNext(0); indexId; indexId = dictTable.indexNext(indexId)) // loop index of table
{
dictIndex = dictTable.indexObject(indexId); // get dictIndex from dictTable
if (!dictIndex.allowDuplicates()) // check if index property allowDuplicates is true or false
{
for (i=1; i<=dictIndex.numberOfFields(); i++) // loop thru fields of index
{
if (dictIndex.field(i) == fieldId) // get fieldId of the current field
return true;
}
}
}
}
return false;

Dictionary

Object can be constructed by “new” word (no Params required). Dictionary class is the general class for getting some informations about every AOT object.

Example usage in AX

Classes\ProjTableType\returnClass

Example code

// This example will show some functionalities of Dictionary class (Only for class related methods).

Dictionary dict = new Dictionary();
int i;
;
for (i = 1; i<=dict.classCnt(); i++) // Loop thru the classes in AOT
{
print dict.classCnt2Id(i); // classid of class
print dict.classFlush(); // Restore class object
print dict.className(dict.classCnt2Id(i)); // name of class
print dict.className2Id(dict.className(dict.classCnt2Id(i))); // will print same value as dict.classCnt2Id(i)
print dict.classNext(dict.classCnt2Id(i)); // classid of the next class after the current class
print dict.classObject(dict.classCnt2Id(i)); // constructs dictClass of the current class object
}
pause;

DictLicenseCode

Object can be constructed by “new” word (Params: LicenseCodeId). Mostly used for finding licensecode group

Example usage in AX

Classes\SysLicenseCodeReadFile\createCodes

Example code

// This example will show some functionalities of DictLicenseCode class

sysDictLicenseCode = new DictLicenseCode(dictionary.licenseCodeCnt2Id(i)); //construct licensecode object
if (sysDictLicenseCode) // if constructed
{
switch (sysDictLicenseCode.group()) // get group of license code ie. System,Partner,Web etc
{
case LicenseCodeGroup::Language:
print sysDictLicenseCode.id(); // get licensecode id and print

break;
}
}

DictMethod

Object can be constructed by “new” word (Params: UtilElementType, Id, Name). Mostly used for accessing method properties such as return type, parameters etc.

Example usage in AX

Classes\SysApplCheck\checkTableFieldPnameMustBeUnique

Example code

// This example will show some functionalities of DictMethod class

DictMethod dictMethod;
;
// If you want to create dictMethod of the type
//
// ClassInstanceMethod
// ClassStaticMethod
// then 2nd parameter will be ClassId
//
// TableInstanceMethod
// TableStaticMethod
// then 2nd parameter will be TableId
dictMethod = new DictMethod(UtilElementType::ClassInstanceMethod,
classNum(CustInPaym),
identifierstr(loadVoucherNum));
print dictMethod.returnType(); // prints return type (void)
print dictMethod.isAbstract(); // prints if the method is abstract (false)
print dictMethod.parameterCnt(); // prints parameter count of method (0)
print dictMethod.accessSpecifier(); // prints access specifier (public)
print dictMethod.returnId(); // prints EDT id of return type (in this case 0 = void)
print dictMethod.noParms(); // prints 1 which means there is no parameter required for that method
print dictMethod.parentId(); // in this case prints classid (72) (because of method is class instance method)
pause;

DictRelation

Object can be constructed by “new” word (Params: TableId). Mostly used for determining relation of two tables.

Example usage in AX

Classes\SysSeachResultForm\getMainRecord

Example code

// This example will show some functionalities of DictRelation class

private static Common getMainRecord(int iPMainTableId, int iPLinesTableId, Common CPLinesTableRecord)
{
DictRelation CDictRelation;
int i;
DictField CMainDictField;
DictField CLineDictField;
Query CQuery = new Query();
QueryRun CQrun;
Common CMainTableRecord;

;

CDictRelation = new DictRelation( iPLinesTableId ); // construct dictrelation according to line table
CDictRelation.loadTableRelation( iPMainTableId ); // give tableid to be found relation

// get list of fields that one table has to another
// and build query to main table
CQuery.addDataSource( iPMainTableId );
if (CDictRelation.lines())
{
for (i = 1; i <= CDictRelation.lines(); i++)
{
CMainDictField = new DictField(iPMainTableId, CDictRelation.lineExternTableValue(i)); // Here is the point, that job of dictrelation begins. CDictRelation.lineExternTableValue(i) returns maintable relation field
CLineDictField = new DictField(iPLinesTableId, CDictRelation.lineTableValue(i)); // And CDictRelation.lineTableValue(i) returns line table relation field
CQuery.dataSourceTable( iPMainTableId ).addRange( CMainDictField.id() ).value( queryValue( CPLinesTableRecord.( CLineDictField.id() ) ) );
}
}
CQrun = new QueryRun(CQuery);
CQrun.next();
CMainTableRecord = CQrun.get(iPMainTableId);

return CMainTableRecord;
}

DictSecurityKey

Object can be constructed by “new” word (Params: SecurityKeyId). Mostly used for checking if the user has securitykey access.

Example usage in AX

Classes\Global\hasSecurityKeyAccess

Example code

// Below method will explains DictSecurityKey class briefly

static boolean hasSecuritykeyAccess(securityKeyId securityKeyId, AccessType neededAccessLevel)
{
DictSecurityKey dsk=new DictSecurityKey(securityKeyId); // Construct class
;

if (dsk == null) // if not constructed then there is no such a securityKey
return false;
else
return dsk.rights() >= neededAccessLevel; // rights method will return current user’s AccessType (eg. NoAccess or View etc) to SecurityKey
}

DictTable

Object can be constructed by “new” word (Params: TableId). Mostly used for getting information and calling methods of table.

Example usage in AX

Classes\SysApplCheck\checkForm

Example code

// Below method will explains DictTable class briefly

DictTable dictTable;
CustTable custTable;
CustAccount accountNum = 'CustomerNo1';
;
select custTable
where custTable.AccountNum == accountNum; // we will use that record below
// we can construct table, map, view
dictTable = new DictTable(tablenum(CustTable)); // Construct dictTable class (here we will use CustTable as an example)
print dictTable.callObject(identifierstr(countryName),custTable); // callObject will execute 'countryName' instance method using the selected record above and returns value/object (according to the method return type)
print dictTable.callStatic(identifierstr(blocked),accountNum); // this will execute 'blocked' static method and returns value/object (according to the method return type)
print dictTable.fieldGroupCnt(); // prints count of fieldgroup in that table (CustTable)
CustTable = dictTable.makeRecord(); // makes a new record
dictTable.isMap(); //is object type of Map?
dictTable.isView(); //is object type of View?
dictTable.isTmp(); // (if it is table) is it temporary table?
print dictTable.titleField1(); // prints titlefield1 (fieldnum) property of table (is it is table)
pause;

DictType

Object can be constructed by “new” word (Params: extendedTypeId). Mostly used for getting information about any type.

Example usage in AX

Classes\AifMessage\validateString

Example code

// Below method will explains DictType class briefly

DictType dictType = new DictType(extendedTypeNum(CustAccount));
;
print global::extendedTypeId2name(dictType.extend()); // prints CustVendAC (ie. extend type of dictType)
print dictType.label(); // prints "Customer account" which is label of dictType
dictType.relationObject(); // returns DictRelation which contains relation to table which is specified in EDT
dictType.stringLen(); // if it is the type of string, then prints string length of dictType
dictType.configurationKeyId(); // returns "configurationKeyId" of dictType
pause;

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...