Adding an Address to an Existing Customer via the Customer Service in AX 2012

By Becky Newell | October 23, 2013

It isn't obvious but to add an address to an existing customer via a service call in AX 2012, call the update operation on the Customer service.  The code below demonstrates how to change a field on the customer record and add a new address to the customer using a console application:

static void Main(string[] args)

{

string customer = "123456";

AxdCustomer foundCustomer = null;

CallContext context = new CallContext();

context.Company = "xyz";

CustomerServiceClient proxy = new CustomerServiceClient();

try

{

foundCustomer = proxy.read(context, Program.readCritera(customer));

AxdEntity_DirParty_DirPartyTable returnedPartyTable = foundCustomer.CustTable.DirParty;

Program.addAddressToExistingCustomer(foundCustomer);

}

catch (Exception e)

{

Console.WriteLine(e.Message);

}

}

private static void addAddressToExistingCustomer(AxdCustomer customer)

{

CustomerServiceClient proxy = new CustomerServiceClient();

CallContext context = new CallContext();

context.Company = "xyz";

AxdEntity_CustTable custTable = customer.CustTable;

AxdCustomer axdCustomer2 = new AxdCustomer();

axdCustomer2.ValidTimeStateType = customer.ValidTimeStateType;

axdCustomer2.ValidTimeStateTypeSpecified = true;

axdCustomer2.ValidAsOfDateTime = customer.ValidAsOfDateTime;

axdCustomer2.ValidFromDateTime = customer.ValidFromDateTime;

axdCustomer2.ValidToDateTime = customer.ValidToDateTime;

AxdEntity_CustTable custTableNew = new AxdEntity_CustTable();

custTableNew._DocumentHash = custTable._DocumentHash;

custTableNew.RecId = custTable.RecId;

custTableNew.RecVersion = custTable.RecVersion;

custTableNew.action = AxdEnum_AxdEntityAction.update;

custTableNew.actionSpecified = true;

custTableNew.CreditMax = custTable.CreditMax + 10;

custTableNew.CreditMaxSpecified = true;

custTableNew.CustGroup = custTable.CustGroup;

//Update the postal addresses

AxdEntity_DirParty_DirOrganization dirPartyTable = (AxdEntity_DirParty_DirOrganization)custTable.DirParty;

AxdEntity_DirParty_DirOrganization organizationNew = new AxdEntity_DirParty_DirOrganization();

organizationNew.RecId = dirPartyTable.RecId;

organizationNew.RecIdSpecified = true;

organizationNew.RecVersion = dirPartyTable.RecVersion;

organizationNew.RecVersionSpecified = true;

organizationNew.action = AxdEnum_AxdEntityAction.update;

organizationNew.actionSpecified = true;

AxdEntity_DirPartyPostalAddressView newAddress = new AxdEntity_DirPartyPostalAddressView();

newAddress.LocationName = "New Location Name";

newAddress.Street = "9999 NE 5th St";

newAddress.City = "Beverly Hills";

newAddress.State = "CA";

newAddress.CountryRegionId = "USA";

newAddress.ZipCode = "90210";

newAddress.Roles = "Delivery";

newAddress.action = AxdEnum_AxdEntityAction.create;

newAddress.actionSpecified = true;

organizationNew.DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView { newAddress };

custTableNew.DirParty = new AxdEntity_DirParty_DirPartyTable { organizationNew };

axdCustomer2.CustTable = new AxdEntity_CustTable { custTableNew };

try

{

proxy.update(context, Program.readCritera("123456"), axdCustomer2);

Console.Write("Worked");

Console.ReadLine();

}

catch (Exception e)

{

Console.WriteLine("Failed");

Console.ReadLine();

}

}

private static EntityKey[] readCritera(string customerAccount)

{

AxdEntity_CustTable custTable = new AxdEntity_CustTable();

EntityKey[] entityKeyList = new EntityKey;

EntityKey key = new EntityKey();

KeyField[] keyFields = new KeyField;

KeyField keyField = new KeyField();

keyField.Field = "AccountNum";

keyField.Value = customerAccount;

keyFields = keyField;

key.KeyData = keyFields;

entityKeyList = key;

return entityKeyList;

}

Related Posts


Under the terms of this license, you are authorized to share and redistribute the content across various mediums, subject to adherence to the specified conditions: you must provide proper attribution to Stoneridge as the original creator in a manner that does not imply their endorsement of your use, the material is to be utilized solely for non-commercial purposes, and alterations, modifications, or derivative works based on the original material are strictly prohibited.

Responsibility rests with the licensee to ensure that their use of the material does not violate any other rights.

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!