Microsoft Dynamics AX Support

This blog contains posts by the Microsoft Dynamics AX Support Teams Worldwide

Updating customers via the Customer AIF service

We have received many questions in AX 2009 around how to update customers using the customer service in AIF.  People report errors like the following while trying to do updates:

“The record in Address with record id <12345789> cannot be updated because the record has changed since last read.”

To update customers set the DocumentHash of the CustTable entity to itself, and the RecId and RecVersion fields of the AddressRelationship and AddressRelationshipMap to themselves so that AIF is aware of which record in each table you want to update.  In your code the update call should look something like the following:

CustomerServiceClient proxy = new CustomerServiceClient();

 

foreach (AxdEntity_CustTable custTable in this.customer.CustTable){

custTable.CellularPhone = “3456789999”;

custTable._DocumentHash = custTable._DocumentHash;

custTable.AddressRelationship[0].RecId = custTable.AddressRelationship[0].RecId;

custTable.AddressRelationship[0].RecVersion = custTable.AddressRelationship[0].RecVersion;

custTable.AddressRelationship[0].AddressRelationshipMap[0].RecId = custTable.AddressRelationship[0].AddressRelationshipMap[0].RecId;

custTable.AddressRelationship[0].AddressRelationshipMap[0].RecVersion = custTable.AddressRelationship[0].AddressRelationshipMap[0].RecVersion;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].RecId = custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].RecId;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].RecVersion = custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].RecVersion;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].State =

“SD”;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].City =

“Onida”;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].County =

“SULLY”;

custTable.AddressRelationship[0].AddressRelationshipMap[0].CustAddress[0].ZipCode =

“57564”;

}

 

try{

this.createEntityKeyList(), customer);

proxy.update(

Console.WriteLine(“The customer was successfully updated”);

}

catch (Exception e) {

Console.WriteLine(e.InnerException.Message);

Console.WriteLine(“The customer was not updated.”);

}

For the file adapter the update portion of your xml file should look something like:

<Customer xmlns=”http://schemas.microsoft.com/dynamics/2008/01/documents/Customer“>
                <CustTable class=”entity” action=”update”>
      <_DocumentHash>140885c43d75ec28cf99c8778b0414e3</_DocumentHash>
                    <AccountNum>1101</AccountNum>
                    <CustGroup>20</CustGroup>
                    <PartyType>Organization</PartyType>
   <AddressRelationship class =”entity” action=”update”>
    <PartyId>191</PartyId>
    <RecId>5637145108</RecId>
    <RecVersion>1</RecVersion>
    <AddressRelationshipMap class=”entity” action=”update”> 
     <RecId>5637145533</RecId>
     <RecVersion>1</RecVersion>
                          <CustAddress class=”entity” action=”update”>
                               <RecId>5637151719</RecId>
                               <RecVersion>623863500</RecVersion>
          <ZipCode>58801</ZipCode>
                          </CustAddress>
    </AddressRelationshipMap>
   </AddressRelationship>
                </CustTable>
            </Customer>