Creating a new table and adding new fields to created table throug X++

Hi,
The below code is helpful to create a new table and add fields to that through X++,

Create a temporary Table:
static void Tbl(Args _args)
{
#aot
#properties
SysDictTable sysdictTable;
treenode trv;
AOTTableFieldList fieldnode;
str prop;
int pos;
;
trv = treenode::findNode(#TablesPath);
//trv.AOTDrop(‘AAA’,1);
trv.AOTadd(‘AAA’);

trv = trv.AOTfindChild(‘AAA’);
trv.AOTcompile(1);
trv.AOTsave();

trv.AOTfindChild(‘AAA’);

fieldnode = trv.AOTfirstChild();
fieldnode.addString(‘AccountNum’);
fieldnode = fieldnode.AOTfindChild(‘AccountNum’);
prop = fieldnode.AOTgetProperties();

pos = findPropertyPos(prop,#PropertyExtendeddatatype); //find right place to put extended data type
pos = strFind(prop,’ARRAY’,pos,strLen(prop));
pos = strFind(prop,’#’,pos,strLen(prop));

prop =strins(prop,extendedTypeId2name(ExtendedtypeNum(CustAccount)),pos + 1);
// insert field of extended data type ‘CustAccount’
fieldnode.AOTsetProperties(prop);

trv.AOTcompile(1);
trv.AOTsave();

trv.AOTRestore(); //to load assigned extended data type properties

sysdictTable = sysdictTable::newTreeNode(trv);
appl.dbSynchronize(sysdictTable.id());

}

Normal Table:
static void newTableCreate(Args _args)

{

TreeNode treeNode;

#AOT

;

treeNode = TreeNode::findNode(#TablesPath);

treeNode.AOTadd(“Table_Test”);

SqlDataDictionary::synchronize();

}

Add fields to created table::

The following example adds an enum type field to the TutorialJournalName table.

{
AOTTableFieldList tfl = infolog.findNode(‘\\Data Dictionary\\Tables\\TutorialJournalName\\Fields’);
;

if (! hasSecuritykeyAccess(securitykeynum(SysDevelopment), AccessType::View))
{
return;
}

if (!tfl.AOTFindChild(‘NewEnum’))
{
tfl.addEnum(‘NewEnum’); // Adds the field NewEnum.
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s