Monday, July 15, 2013

Customer/Vendor Address import in dynamics AX 2012 through X++

15-07-13
Customer and Address import in Dynamics AX 2012 using X++
Problem: I was assigned to a task that import customer and their addresses from Excel sheet. I can import customer through code by using AXCustTable service class but what about addresses. In AX 2012 addresses changed a lot they created new framework for addresses.
Solved: I had gone through lot of websites for customer address import. Every code was almost identical. I am happy with all the codes but one thing the code doesn’t solve it. They all work until you have to fill all the address masters before create new address for customer. In my case I have to create new address not selecting the address for my customer. So I had gone through address framework and created the code for creating addresses as well as customers. So I had liked to share with you.  (But here I covered only the address part electronic address included)
 
Main method creating for address:
    DirPartyRecId                                                partyRecId;
    DirPartyPostalAddressView                          addressView;
    AxLogisticsAddressCountryRegion              axCountryRegion;
    AxLogisticsAddressCounty                           axCounty;
    AxLogisticsAddressState                               axState;
    AxLogisticsAddressZipCode                         axZipcode;
    AxLogisticsAddresssCity                               axCity;
    DirParty                                                          DirParty;
    DirPartyContactInfoView                              contactView;
    LogisticsLocationRecId                                 locationRecid;
    ;
    try
    {
            addressView.CountryRegionId = _CountryRegionId;
            addressView.State   = _state;
            addressView.City    = _city  ;
            addressView.ZipCode = _zipCode;
            addressView.Street  = _street;
            addressView.Party   = _custTable.Party;
            //addressView.Location    =   locationRecid;
                //Separate method find it in below code
            this.processAddress(addressView,_custTable);
            DirParty = DirParty::constructFromPartyRecId(addressView.Party);
                //Creating new address using dir party class
            if( addressView.Street || addressView.ZipCode || addressView.City || addressView.State ||                       addressView.CountryRegionId)
            {
                DirParty.createOrUpdatePostalAddress(addressView,_roleId);
            }
                //Electronic address creaiton
            if(_AccountManagerEmail!='')
            {
                contactView.LocationName = "Delivery email";
                contactView.Locator      = _AccountManagerEmail;
                contactView.Type         = LogisticsElectronicAddressMethodType::Email;
                contactView.Party        = _custTable.Party;
                contactView.IsPrimary    = NoYes::Yes;
                DirParty = DirParty::constructFromPartyRecId(contactView.Party);
                dirParty.createOrUpdateContactInfo(contactView);
            }
            if(_phone!='')
            {
                contactView.LocationName = "Phone";
                contactView.Locator      = _phone;
                contactView.Type         = LogisticsElectronicAddressMethodType::Phone;
                contactView.Party        = _custTable.Party;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);
            }
            if(_teleFax!='')
            {
                contactView.LocationName = "Tele Fax";
                contactView.Locator      = _teleFax;
                contactView.Type         = LogisticsElectronicAddressMethodType::Telex;
                contactView.Party        = _custTable.Party;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);
            }
            if(_url!='')
            {
                contactView.LocationName = "URL";
                contactView.Locator      = _url;
                contactView.Type         = LogisticsElectronicAddressMethodType::URL;
                contactView.Party        = _custTable.Party;
                contactView.IsPrimary    = NoYes::Yes;
                dirParty.createOrUpdateContactInfo(contactView);
            }
    }
    catch (Exception::Error)
    {
        info ("Customers not created");
    }
 
  Process address method:
    AxLogisticsAddressCountryRegion axCountryRegion;
    AxLogisticsAddressCounty        axCounty;
    AxLogisticsAddressState         axState;
    AxLogisticsAddressZipCode       axZipcode;
    AxLogisticsAddresssCity         axCity;
    LogisticsAddresssCity            addressCity; 
    ; 
    if(addressView.CountryRegionId  &&  !LogisticsAddressCountryRegion::exist       (addressView.CountryRegionId))
    {
        axCountryRegion =   new AxLogisticsAddressCountryRegion();
        axCountryRegion.parmCountryRegionId(addressView.CountryRegionId);
        axCountryRegion.parmCurrencyCode(_custTable.Currency);
        axCountryRegion.parmAddrFormat("0014");
        axCountryRegion.save();
    }
    if (addressView.State && !LogisticsAddressState::exist(addressView.CountryRegionId, addressView.State))
    {
        axState =   new AxLogisticsAddressState();
        axState.parmCountryRegionId(addressview.CountryRegionId);
        axState.parmStateId(addressView.State);
        axState.parmName(addressView.State);
        axstate.save();
    }
    if (addressView.County && !LogisticsAddressCounty::exist(addressView.CountryRegionId, addressView.State, addressView.County))
    {
        axCounty    =   new AxLogisticsAddressCounty();
        axCounty.parmCountryRegionId(addressview.CountryRegionId);
        axCounty.parmStateId(addressview.State);
        axCounty.parmCountyId(addressView.County);
        axCounty.parmName(addressview.County);
        axCounty.save();
    }
    if(AddressView.City &&  !Logisticsaddressscity::exist(Logisticsaddressscity::find(addressView.city).RecId))
    {
        /*axcity  =   new AxLogisticsAddresssCity();
        axCity.parmCountryRegionId(addressview.CountryRegionId);
        axCity.parmCountyId(addressview.County);
        axCity.parmName(addressView.City);
        axCity.parmStateId(addressview.State);
        axCity.save();*/
        addressCity.CountryRegionId =   addressView.CountryRegionId;
        addressCity.Name    =   addressView.City;
        addressCity.StateId =   addressView.State;
        addressCity.doInsert();
    }
    if (addressView.ZipCode &&  !LogisticsAddressZipCode::exist(addressView.ZipCode))
    {
        axZipcode   =   new AxLogisticsAddressZipCode();
        axZipcode.parmCity(addressview.City);
        axZipcode.parmCityRecId(Logisticsaddressscity::find(addressView.city).RecId);
        axZipcode.parmCountryRegionId(addressview.CountryRegionId);
        axZipcode.parmCounty(addressView.County);
        axZipcode.parmState(addressView.State);
        axZipcode.parmStreetName(addressView.Street);
        axZipcode.parmZipCode(addressView.ZipCode);
        axZipcode.save();
    }

2 comments:

  1. how to update a format of address In AX 2012 ?

    ReplyDelete
  2. how to update a format of address In AX 2012 ?

    ReplyDelete