In Dynamics (AX/365) there is a concept of
"Valid Time State Tables" that contain date effective data. In this post, you'll find code to list all the previous addresses tied to an individual record.
If you are not familiar with this type of data, the simplest example to wrap your head around is postal addresses. If you are, just skip the next paragraph.
Let's say you send an order/invoice to a customer at
123 South St on 1/1/2017. Then a few months later, the customer moves locations to
456 North Ave and you update their address in Dynamics. All is good so far. Then for some reason you need to reprint the 1/1/2017 invoice...well that address record for
123 South St still exists in the database, but it's "ValidTo" is in the past. This way you have the historical addresses.
This sample job below takes an address and loops through the older versions of that address. This method can be used to seek any data in valid time state tables though.
static void AlexOnDAXFindOldAddresses(Args _args)
{
CustTable custTable = CustTable::find("ZZZZ");
LogisticsPostalAddress logisticsPostalAddress;
LogisticsPostalAddress logisticsPostalAddressOld;
utcDateTime utcMinVal = DateTimeUtil::minValue();
utcDateTime utcMaxVal = DateTimeUtil::maxValue();
// Active address
logisticsPostalAddress = CustTable.postalAddress();
info("Current: " + logisticsPostalAddress.Address);
setPrefix("Previous Addresses");
// This will loop through the inactive addresses in order
while select validTimeState(utcMinVal, utcMaxVal) logisticsPostalAddressOld
order by ValidTo desc
where logisticsPostalAddressOld.Location == logisticsPostalAddress.Location &&
logisticsPostalAddressOld.RecId != logisticsPostalAddress.RecId
{
info(strFmt("%1 [%2-%3]",
logisticsPostalAddressOld.Address,
logisticsPostalAddressOld.ValidFrom,
logisticsPostalAddressOld.ValidTo));
}
}