API
Version 7.x > API > Setting up a custom event handler to delete a customer when a user is deleted View modes: 
User avatar
Member
Member
SYDA Developer LV - 12/3/2013 8:33:48 AM
   
Setting up a custom event handler to delete a customer when a user is deleted
The only customers that should exist on our site should be attached to a user account. At present, when an account is deleted in CMS Desk, the customer that was attached to the user is not deleted.

This is not a problem.

The problem is that we have attempted to write a custom global event handler to delete the customer when a user is deleted. Unfortunately, the customer's CustomerUserID property is given a value of null before an event handler that triggers on UserInfo.TYPEINFO.Events.Delete.Before can fire, and so we are unable to find the customer associated with the user using CustomerInfoProvider.GetCustomerInfoByUserID. While a conversation with Kentico support has convinced me that our only options are to add a column to the CMS_User table and/or customer table OR to use CustomerInfoProvider.GetCustomers, I was wondering if anyone had been able to find another solution to the problem. I'd prefer to not use either method, to be honest, but I may end up using GetCustomers if I have to.

User avatar
Member
Member
SYDA Developer LV - 12/3/2013 8:35:47 AM
   
RE:Setting up a custom event handler to delete a customer when a user is deleted
Full code of our event handler:

private void UserInfo_Delete_Before(object sender, ObjectEventArgs e)
{
if (e.Object != null)
{
UserInfo userInfo = (UserInfo)e.Object;
int userID = userInfo.UserID;

CustomerInfo customerInfo = CustomerInfoProvider.GetCustomerInfoByUserID(userID);

if (customerInfo != null)
{
CustomerInfoProvider.DeleteCustomerInfo(customerInfo.CustomerID);
LogMessage("Customer id " + customerInfo.CustomerID + " has been deleted with User:" + userID);
}
}
}

User avatar
Member
Member
kentico_edwardh - 12/10/2013 6:46:57 PM
   
RE:Setting up a custom event handler to delete a customer when a user is deleted
Hello,

There is no UserID for the E-commerce customers; there is only the CustomerID which has no relation to the actual UserID since users and customers are two separate objects. You can use the methods below to delete both the Customer and the User records for a given user which doesn’t rely on there ID.

private bool DeleteCustomer()
{
// Prepare the parameters
string where = "CustomerLastName LIKE N'My New%'";

// Delete user
UserInfo user = UserInfoProvider.GetUserInfo("My new user");
UserInfoProvider.DeleteUser(user);

// Get the data
DataSet customers = CustomerInfoProvider.GetCustomers(where, null);
if (!DataHelper.DataSourceIsEmpty(customers))
{
foreach (DataRow customerDr in customers.Tables[0].Rows)
{
// Create object from DataRow
CustomerInfo deleteCustomer = new CustomerInfo(customerDr);

// Delete the customer
CustomerInfoProvider.DeleteCustomerInfo(deleteCustomer);
}
return true;
}
return false;
}


private bool DeleteUser()
{
// Get the user
UserInfo deleteUser = UserInfoProvider.GetUserInfo("MyNewUser");

// Delete the user
UserInfoProvider.DeleteUser(deleteUser);

return (deleteUser != null);


Otherwise, you could consider using the ObjectEvents.Delete.Before event instead of the UserInfo.TYPEINFO.Events.Delete.Before event as you have planned, since both the User and Customer are objects. The CustomerUserID should still be available in this handler.