Update Customer Primary Address – Dynamics AX 2012

Dynamics AX is a powerful ERP system that is ideal for mid to large size manufacturing, retail, and logistics organizations. I ran into a situation where a bunch of new customers were imported into our AX from an Excel file, but the address in the file did not contain the zip code, so AX defaulted to a blank zip code for about 500 customers.  And because I don’t want to update 500 customer manually, I created the following job to update multiple customer account records with the correct zip code. The X++ job below can be used to update an existing primary address or any part of the address, based on the customer account number.

This code can be manipulated to change any part of the address including street name, city, state, and country/region code. This is the first of many posts related to MS Dynamics AX. Stay tuned for more updates!

static void updateCustomerAddress(Args _args) 
{ 
   container roles;  
   DirParty dirParty; 
   DirPartyPostalAddressView addressView; 
   LogisticsPostalAddress address; 
   CustTable custtable; 
   DirPartyLocation partyLocation; 
 
ttsBegin; 
   
   custtable = Custtable::find("C009999");
   address = DirParty::primaryPostalAddress(CustTable.Party); 
   address = DirParty::findPostalAddressByRole(CustTable.Party, LogisticsLocationRoleType::Business); 
   address.ZipCode = "99999"; 
   addressView.IsPrimary = NoYes::Yes;
   addressView.Party = CustTable.Party; 
   addressview.initFromPostalAddress(address); 
   DirParty = DirParty::constructFromPartyRecId(CustTable.Party); 
   roles = [LogisticsLocationRole::findBytype(LogisticsLocationRoleType::Business).RecId];
   DirParty.createOrUpdatePostalAddress(addressView,roles);

ttsCommit; 

info("Job Completed."); 
}