Tuesday, 22 October 2019

AX : Get contact person name and contact number of purchase order delivery Address


Recently I got the requirement to get the contact person name and contact number of purchase order delivery Address. While searching on the internet I come across several line of code to get the company, customer, vendor contact person name and contact number but I didn’t find any code of my query. So I decide to document it for others. So here we go. Look at the below image where Purchase order Line details tab contains the delivery Address in my case it iSITE -2



So the contact person name of delivery address can be found at Organization administration > legal Entities > Address tab  >  select the delivery Address of which we need to get contact person  >  click on Edit button we will get the screen shown below 


So to get the above contact person name which is under description field of delivery address Site – 2. Following code is going to do a trick :

static void getDeliveryAddressContactPersonName(Args _args)
{
    LogisticsElectronicAddress  electronicAddress;
        purchTable                  purchTableLocal;
        LogisticsLocation           logisticsLocation1, logisticsLocation2;
       
        purchTableLocal = purchTable::find("PO-0000184");
        logisticsLocation2 = LogisticsLocation::find(purchTableLocal.deliveryAddress().Location);
   
        select ParentLocation, RecId from logisticsLocation1 where
            logisticsLocation1.ParentLocation == logisticsLocation2.RecId;
   
        electronicAddress = LogisticsElectronicAddress::findByLocationAndType(logisticsLocation1.RecId, LogisticsElectronicAddressMethodType::Phone);  
        
        info(strFmt("Contact number: %1", electronicAddress.Locator));     // return contact Person Phone Number
        info(strFmt("Contact Person : %1", electronicAddress.Description));  // return Contact Person Name
}

When we execute above job we  get the below result