web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Delete address through code

(0) ShareShare
ReportReport
Posted on by

Hello,

I am trying to delete an address in ax 2012 through code, i am trying to find the addresses which doesn't have any open orders and delete them. All i get is AOS validation error. I debugged the entire process of deletion and found its checking 114 tables for record reference before deletion. Is there a way to delete this kind of addresses. i also tried to expire the address and delete them but failed to do so. Please let me know if we can do this  

static void NK_AddressCleanUp(Args _args)
{
LogisticsLocation location;
SalesLine salesLine;
LogisticsPostalAddress logisticsPostalAddress;
utcDateTime toDate = DateTimeUtil::newDateTime(mkDate(4, 5, 2015),0);

ttsBegin;

while select forupdate location
where location.createdDateTime >= toDate && location.createdDateTime <= DateTimeUtil::newDateTime(mkDate(4, 5, 2015), 72000)
join forUpdate logisticsPostalAddress
where logisticsPostalAddress.Location == location.RecId
{

select RecId, SalesId from salesLine where salesline.DeliveryPostalAddress == logisticsPostalAddress.RecId && salesline.SalesStatus != SalesStatus::Invoiced;

if(salesLine.RecId == 0 && location.validateDelete())
{
info(strFmt("%1--%2--%3", location.Description, location.RecId, salesLine.SalesId));
location.Delete();
}
}
ttsCommit;
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    301,130 Super User 2025 Season 2 on at

    Hi Naga,

    Do you have any idea where all addresses and locations might be used? It is not only on the sales lines. The LogisticsPostalAddress contains records for all addresses in the system. Also for customers, vendors, warehouses, legal entities, projects, etc.

    So, you cannot just simply check only the not invoiced sales lines. The addresses can be used anywhere. E.g. a delivery address is set on a customer. only the reference record ID of this same address could be stored on the sales line. Luckily AX 2012 has more validations to prevent you doing incorrect delete actions on this table.

  • Community Member Profile Picture
    on at

    Hello Andre, thanks for your reply. I have seen what you explained that address is being used in many places. In my case the address is created when a salesorder is created using an integration site (we have service to consume the xml and generate salesorder in AX 2012). So when the order is created our code is customized to create the address if there is not one for that customer. but these are one time addresses and will not be used again. So we have to  cleanup those addresses once the orders are closed(invoiced). How do you recommend to approach in deleting this kind of addresses. your help is highly appreciated.  

  • Verified answer
    André Arnaud de Calavon Profile Picture
    301,130 Super User 2025 Season 2 on at

    Hi Naga,

    Usually it will not be cleaned as there might be historical data related to packing slips and invoices where also the same address reference can be used. So deleting your 'one-time' addresses might be blocked by references on packing slip and invoice journals.

  • Community Member Profile Picture
    on at

    yes we will delete the addresses after the 4 months from the date of creation, until then there are used for audit history. But some how we have to get rid of these addresses since one million addresses  are created every month. This will increase the unnecessary load if we don't cleanup.

  • André Arnaud de Calavon Profile Picture
    301,130 Super User 2025 Season 2 on at

    There are delete actions on the tables which will cause AX to check for existing references or do cascade delete in other tables. You can try to debug or use the trace parser to find out which table(s) is having a reference. You can decide to remove some delete actions and replace it with own logic on the validateDelete() method. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans