Update Customer through Customer Service (C# app .net 4+) fails with 'Invalid entity item'

In Dynamics client app, system administration/exceptions the subsystem is:
Line=1, Pos=13033, Xpath=/Customer/CustTable[1]/DirParty[1]

Exception is thrown during the .update command. Exception ‘Invalid entity item’ is useless.

All I need to do is update the address of an existing customer. If I can just get this working, I can most likely update any other fields needed.

Following is my code:

private static void UpdateCustomer(AxdCustomer customer)
{
string sCustID = customer.CustTable[0].AccountNum;

CustomerServiceClient csClient = new CustomerServiceClient();
CustomerService.CallContext callContext = new CustomerService.CallContext();
callContext.Company = sCompany;

AxdEntity_CustTable custTable = customer.CustTable[0];

AxdEntity_DirParty_DirOrganization dirOrg = new AxdEntity_DirParty_DirOrganization();
AxdEntity_DirPartyPostalAddressView dirAddress = new AxdEntity_DirPartyPostalAddressView();

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 = CustomerService.AxdEnum_AxdEntityAction.update;
custTableNew.actionSpecified = true;

/////////// update address
dirAddress.Street = CustDetails.sAddress;
dirAddress.CountryRegionId = “USA”;
/////////

custTableNew.InterCompanyDirectDelivery = CustomerService.AxdExtType_InterCompanyDirectDelivery.Yes;
custTableNew.InterCompanyDirectDeliverySpecified = true;

custTableNew.CustGroup = custTable.CustGroup;

custTableNew.DirParty = new AxdEntity_DirParty_DirPartyTable[1] { dirOrg };
custTableNew.DirParty[0].DirPartyPostalAddressView = new AxdEntity_DirPartyPostalAddressView[1] { dirAddress };

axdCustomer2.CustTable = new AxdEntity_CustTable[1] { custTableNew };

try
{
csClient.update(callContext, ReadCriteria(sCustID), axdCustomer2);
Console.WriteLine(“Customer Updated!”);
}

catch (Exception e)
{
Console.WriteLine("Update Customer Failed - " + e.Message);
}
}

It seems that you’re trying to add a party without any fields filled in. What are you trying to achieve by that?
Why are you creating a new AxdCustomer and trying to copy all the data, instead of just updating the AxdCustomer object that you get in the parameter? If you avoid the attempt to copy the data, you’ll get rid of the bugs that you have there.

I tried just modifying the original ‘customer’ and original CustTable passed into the function, but I’m still getting the same exception: ‘Invalid entity item’, same subsystem: Line=1, Pos=13033, Xpath=/Customer/CustTable[1]/DirParty[1].

Just thought of something else. In my above example, there’s no flag or ID set to indicate ‘which’ address I am trying to update. What if a customer has multiple addresses? What do you set to indicate which address you are trying to update?

I think it always works with the primary address. You can look at the implementation of the service to be sure.

Regarding getting the same error again, please show us your new code and review if the party object is correctly filled in (you can use the debugger for that).