copy table from one company to another

hey all tell me where i m wrong in this code i want to copy the table data from one company to anoter company using the table variable in ax 2009 pls help.

below is code for the job pls correct my error

static void CompanyCopyTable(Args _args)

{

Dialog dialog;

DialogField field,field1,field2;

str cname,cname1;

common c;

CustoTable custTable1;

CustoTable custTable2,custTable3;

int cid,cgrp;

str bl,name,pl,dl;

Args args = new Args();

;

dialog = new Dialog(“CompanyCross”);

dialog.addText(“Select the source company:”);

field = dialog.addField(typeid(dataAreaId));

dialog.addText(“Select the destination company:”);

field1 = dialog.addField(typeid(dataAreaId));

dialog.run();

if(dialog.closedOk() == true)

{

cname = field.value();

info(cname);

cname1 = field1.value();

info(cname1);

changecompany(cname1)

{

while select custTable2

{

ttsbegin;

select forupdate custTable2;

if(custTable2.validateDelete())

custTable2.delete();

ttscommit;

}

}

changecompany(cname)

{

while select custTable1

{

custTable3.data(custTable1);

changecompany(cname1)

{

ttsbegin;

//uf2buf(custTable1, custTable2);

custTable2.data(custTable3);

if (!custTable2.validateWrite())

{

throw Exception::Error;

}

custTable2.insert();

ttscommit;

}

}

}

}

}

i m new to ax i also want to know the solution of this…

I’m surprised that anybody needs such code at all. If you want to share customers, use virtual companies. It’s also unusual to delete all customers.

Anyway, I would rewrite the core part to something like this (not tested):

changecompany(companyFrom)
{
	while select custTableFrom
	{
		changecompany(companyTo)
		{
			custTableTo = null;
			buf2buf(custTableFrom, custTableTo);

			if (!custTableTo.validateWrite())
			{
				throw Exception::Error;
			}
			
			custTableTo.insert();
			ttscommit;
		}
	}
}

You also shouldn’t run the data manipulation on client (use RunBase framework instead). And this part, using the same variable, is highly suspicious:

while select custTable2
{
    ttsbegin;
    select forupdate custTable2;

thanx very much for your help its highly appericiated…

and i just wanted to test few tables i was not deleting the customers its custotable and not custtable…

anyways thanx for you help again u are a saviour and you always read each and every post and reply it genuinely and i really appericate that thanks again.

All right, I really didn’t noticed it’s CustoTable. Maybe a better naming convention would make your code easier to understand. :slight_smile:

By the way, I really don’t read every post. A lot about development, but my time and knowledge have their limits.