November Happy Hour will be moved to Thursday December 5th.

How to delete a CustomerContact programatically?

Vote:
 

Hello there,

I need to delete CustomerContacts from my EpiServer Commerce 7.5. I have the following code to do that:

CustomerContact c = CustomerContext.Current.GetContactById(contacts.First().ContactId);
                foreach(var address in c.ContactAddresses)
                {
                    c.DeleteContactAddress(address);
                }
                BusinessManager.Delete(c);

However, when going through the BusinessManager.Delete function, an exception is raised with an InnerException regarding of foreign key constraints in the Database (quite normal considering contacts may have associated purchases, addresses, shipments and a quite long chain of dependencies I guess).

Has someone ever deleted a CustomerContact programatically? If so, how to do it cleanly and smoothly?

Thank you and kind regards

#90316
Sep 05, 2014 13:04
Vote:
 

Mediachase.Commerce.Customers.CustomerContact cc = customerContext.GetContactById(YourGuid);
Mediachase.BusinessFoundation.Data.Business.BusinessManager.Delete(cc);

will delete the contact but if Contact have some orders then before that you have to delete all the orders first.

#90324
Sep 05, 2014 17:04
Vote:
 

Could you try to set PreferredBillingAddressId and PreferredShippingAddressId  to null before deleting it.

ContactEntity contact = (ContactEntity)BusinessManager.Load("Contact", customerContact.PrimaryKeyId);
if (contact != null)
{
contact.PreferredBillingAddressId = null;
contact.PreferredShippingAddressId = null;
BusinessManager.Update(contact);
}

BusinessManager.Delete("Contact", customerContact.PrimaryKeyId);

#90366
Sep 08, 2014 13:06
Vote:
 

I might be late to the party but you can call this to delete contact and all belonged objects:

DeleteEntityWithDependsRequest request = new DeleteEntityWithDependsRequest(this.MetaClassName, custId, eRelatedEntityDeleteMode.Delete);
BusinessManager.Execute(request);

Regards.

/Q

#111718
Oct 19, 2014 19:57
Vote:
 

Had the same problem and after reading this thread I ended up with this code that worked:

var contact = (ContactEntity)BusinessManager.Load("Contact", (PrimaryKeyId)currentContact.PrimaryKeyId);
if (contact != null)
{
contact.PreferredBillingAddressId = null;
contact.PreferredShippingAddressId = null;
BusinessManager.Update(contact);
}

var request = new DeleteEntityWithDependsRequest("Contact", (PrimaryKeyId)currentContact.PrimaryKeyId, eRelatedEntityDeleteMode.Delete);
BusinessManager.Execute(request);

#115129
Jan 09, 2015 15:07
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.