Interview Questions

Sunday, February 22, 2015

Delete all companies through code (x++) in AX 2012

static void DeleteCompanyAccountsByXPP(Args _args)
{
DataArea dataArea;
dataAreaId dataAreaId = 'USMF';
container virtualCompanies;
boolean validateDelete;
;

select dataArea where dataArea.id == dataAreaId;

if(dataArea)
{
validateDelete = xDataArea::validateDelete(dataArea);
if(validateDelete)
{
//Find virtual companies which are only assoicated with this
//deleted real company

virtualCompanies=xDataArea::findVirtualCompaniesAffected(dataAreaId);
if (xDataArea::delete(dataAreaId))
{
xDataArea::resyncOnRealCompChange(virtualCompanies);
}
else
{
// this.mark(o);
xDataArea::reloadTableCollections();
}
}
}
else
{
throw error(strfmt("Invalid company accounts %1",dataAreaId));
}
info("done");
}