How to Create a New Table by using X++ code

Hai,

Is it possible to create a new Table by using a X++ code.I read some posts How to create a Table index,but i didn;t find the Creation of Table.Help me any one

Thanks & Regards

Naresh.D

This may help you.

static void newTableCreate(Args _args)

{

TreeNode treeNode;

#AOT

;

treeNode = TreeNode::findNode(#TablesPath);

treeNode.AOTadd(“Table_Test”);

SqlDataDictionary::synchronize();

}


Object ID Allocation
Hai sir,

It is ok but every time it shows following message.i didn’t understad the meaning.is it recommonded to create a table like this way.

Another doubt is how to create a fields in that table .give an example.

Thanks & Regards

Naresh.D


You are about to reserve an object ID from the pool for ‘Tables’ on a shared Team Server, which is a limited resource. You will get this notification for each ID requested.

If you do not intend to have the new object(s) under version control at this point in time, click No, disable version control and repeat the operation.

If you do want to have the new object(s) under version control and are importing many objects, you may want to click No to abort the operation, restart Dynamics AX with the command-line parameter, ‘-silentidrequest=1’, which effectively disables this dialog box, and repeat your import operation. Remember to remove the command-line parameter again.

Are you sure you want to continue?


Yes No

To add fields, use AOTTableFieldList class. Here is an example extracted from Developers guide -


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.
}
}


Hi Naresh,

This message is self explanatory.

Since you have enabled TFS, you are getting this prompt. If you add the command line parameter, you will not get this prompt. Obviously you have to restart Ax after adding this parameter.

Alternatively you can disable TFS.

Best wishes,

Hai sir,

sorry,i didn’t understand about TFS.could u please tell me what is the meaning of TFS and how to disable the TFS.Am new for Axapta.

Thanks & Regards

Naresh.D

Static void newTablewithField(args_args)

{

TreeNode treenode;

AOTTableFieldList fieldlist;

#AOT

;

treenode = TreeNode ::findNode(#TablePath);

treeNode.AoTadd(“TableName”);

sqlDataDictionary::Synchronize();

treeNode.AoTFindChild(TableName);

filedlist = treenode::AOTfieldchild(TableName).AOTfindchild(‘Fileds’);

fieldList.addString(‘FieldName’);

fieldList.addInteger(‘FieldName’);

fieldList.addReal(‘FieldName’);

.

.

.

Print " Table Created";

pause;

}

I was writing a unit test class and used the below code to create a table using X++ code. Hope the below code helps.

SysDictTable sysdictTable;

treenode trv;

AOTTableFieldList fieldnode;

#AOT

trv = treenode::findNode(#TablesPath);

trv.AOTadd(

‘MS_IT_TestCompanyAddress’);

trv = trv.AOTfindChild(

‘MS_IT_TestCompanyAddress’);

trv.AOTcompile(

1);

trv.AOTsave();

trv.AOTfindChild(

‘MS_IT_TestCompanyAddress’);

fieldnode = trv.AOTfirstChild();

// adding fields

fieldnode.addString(

‘GLCompanyAddressNbr’);

fieldnode.addString(

‘AddressOneText’);

fieldnode.addString(

‘StateProvinceCode’);

fieldnode.addString(

‘PostalCode’);

fieldnode.addString(

‘CityName’);

fieldnode.addString(

‘DistrictName’);

fieldnode.addString(

‘AddressPhoneNbr’);

fieldnode.addString(

‘FaxNbr’);

trv.AOTcompile(

1);

Further, you can disable your version control:

Login to Ax(2012) development mode–> Click on tab ‘Version control’–> Click on ‘version control parameters’–> Choose the enum ‘Disable’ for version control status.

Thanks :slight_smile:

Hi Kranthi ,

How to delete a table thru code … I got a problem like, I cant add a table with one particular name but there is no table with that name in table node under AOT . But in code, I can use that name and even I can use recid but there were no other fields, and I can return table id of that table too … So how to achieve this or how to create a table with that name??

Delete table using code

#AOT

Treenode tableNode;

Treenode table;

tableNode = treenode::findNode(#TablesPath);

table = tableNode.AOTfindChild(‘XXTable’);

table.AOTDelete()