Many a times during Data Migration, we encounter a need to delete all customers/vendors and reload them. But in AX 2012, Customer and Vendor entities have a lot of tables which can contain their related information like addresses, party, location, electronic addresses etc. The below code will help you in such scenarios to delete the customer/vendor completely.
static void DeleteCustDetails(Args _args)
{
LogisticsLocation location;
DirPartyLocation partyLocation;
LogisticsPostalAddress postalAdress;
LogisticsElectronicAddress elecAddress;
CustTable custTable;
DirPartyTable partyTable;
ContactPerson contactPerson;
CustBankAccount custBankAccount;
RecId partyRecId;
ttsBegin;
while select custTable
{
delete_from custBankAccount
where custBankAccount.custAccount == custTable.AccountNum;
delete_from postalAdress
exists join location
where postalAdress.Location == location.RecId
join partyLocation
where partyLocation.Party == custTable.Party &&
location.RecId == partyLocation.Location;
delete_from location
exists join partyLocation
where partyLocation.Party == custTable.Party &&
location.RecId == partyLocation.Location;
delete_from partyLocation
where partyLocation.Party == custTable.Party;
delete_from elecAddress
exists join location
where elecAddress.Location == location.RecId
exists join contactPerson
where contactPerson.ContactForParty == custTable.Party
join partyLocation
where partyLocation.Party == contactPerson.Party &&
location.RecId == partyLocation.Location;
delete_from location
exists join partyLocation
join contactPerson
where partyLocation.Party == contactPerson.Party &&
contactPerson.ContactForParty == custTable.Party &&
location.RecId == partyLocation.Location;
delete_from partyLocation
exists join contactPerson
where partyLocation.Party == contactPerson.Party &&
contactPerson.ContactForParty == custTable.Party;
delete_from partyTable
exists join contactPerson
where contactPerson.ContactForParty == custTable.Party &&
partyTable.RecId == contactPerson.Party;
delete_from contactPerson
where contactPerson.ContactForParty == custTable.Party;
partyRecId = custTable.Party;
custTable.selectForUpdate(true);
custTable.delete();
delete_from partyTable
where partyTable.RecId == partyRecId;
}
ttsCommit;
}