Thursday, January 10, 2013

Ax 2009 Get primary Address

 
//This method tries to get the primary address (Ex: customer address). 
//If not found, tries get the address based on the address Type (Ex: delivery address), 
//else tries to find the first available address.

Private Address address(DirPartyId _partyId, AddressType _addressType)
{
    Address         address;
    DirPartyTable   dirPartyTable;
    DirParty        party;
    ;

    while select  address
        join dirPartyTable
            where dirPartyTable.RecId == address.AddrRecId
            &&    dirPartyTable.TableId == address.AddrTableId
            &&    dirPartyTable.PartyId == _partyId
    {
        party = DirParty::constructFromCommon(dirPartyTable);
        party.initializeAddressInfo(DateTimeUtil::getSystemDateTime());
        if (party)
        {
            if (party.getDirPartyAddress().getAddressRecId()==address.RecId)
                return address;
        }
    }

    if (!address)
    {
        select firstonly address  where address.type == _addressType
            join recId from dirPartyTable
            where dirPartyTable.RecId == address.AddrRecId
            &&    dirPartyTable.TableId == address.AddrTableId
            &&    dirPartyTable.PartyId == _partyId;
    }
    if (!address)
        select firstonly address
            join recId from dirPartyTable
                where dirPartyTable.RecId == address.AddrRecId
                &&    dirPartyTable.TableId == address.AddrTableId
                &&    dirPartyTable.PartyId == _partyId;

    return address;
}