Tuesday, November 29, 2016

Get InventSite Address from InventSite ID in AX 2012

I was trying to find the primary address stored for inventSite and almost all articles pointed to complicated queries or views and it looked like it shouldn't be that difficult.


After wasting about 2 hours, it turns out, there is an easy way to get the address. The LogisticsPostalAddress takes the table record as it is and returns the address which is pretty cool. 

The following job takes inventsiteID and returns the delivery address specified in the invent site. Hope it will help someone.

static void addressTest(Args _args)
{
    InventSite site = InventSite::find("S02");
    LogisticsPostalAddress address = LogisticsLocationEntity::findPostalAddress(site, LogisticsLocationRoleType::None);
    info(address.Address);
}


I could not find a simpler way to get the contact details from here. I am currently using the following, but there should be a better way to achieve it.

private Phone getSitePhone(InventSiteId _inventSiteID)
{
    InventSite site = InventSite::find(_inventsiteID);
    LogisticsElectronicAddress  logisticsElectronicAddress;
    LogisticsLocationEntity locationEntity = LogisticsLocationEntity::findLocation(site, LogisticsLocationRoleType::Delivery, DirUtility::getCurrentDateTime(),true);
    RecId parentRecID= locationEntity.parmLocationRecId();
   LogisticsLocation           logisticsLocation;
   if (parentRecID )
   {
      select logisticsLocation
           where  logisticsLocation.ParentLocation == parentRecID
           join logisticsElectronicAddress
               where logisticsElectronicAddress.Type     == LogisticsElectronicAddressMethodType::Phone &&
                   logisticsElectronicAddress.location == logisticsLocation.RecId;
   }
   return logisticsElectronicAddress.Locator;
}

No comments:

Post a Comment