Try our conversational search powered by Generative AI!

BusinessManager.Delete() doesn't work

Vote:
 

Hello,

I would like to implement a function for deleting users. For this purpose, I have written the following piece of code:

ContactEntity target =
(ContactEntity)BusinessManager.Load("Contact", (PrimaryKeyId)entityObject.PrimaryKeyId);
if (target != null)
{
target.PreferredBillingAddressId = new PrimaryKeyId?();
target.PreferredShippingAddressId = new PrimaryKeyId?();
BusinessManager.Update((EntityObject)target);
}

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

Apart from the BusinessManager.Delete() method, I also used other methods:

BusinessManager.Delete(entityObject);

or

var contact = entityObject.CastTo<CustomerContact>(null);
contact.DeleteCustomerContactOnly();


Each of them was returning a Null Exception. After debugging, I noticed that this is happening because the MapUserKey class has a empty "_converters" list. It's worth adding that when I call the MapUserKey class explicitly in my application, it works correctly, and the "_converters" list has 5 elements.

Stack Trace:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Mediachase.Commerce.Customers.MapUserKey.ToUserKey(String typedUserKey)
   at Mediachase.Commerce.Customers.CustomerContext.GetUserForContactId(PrimaryKeyId customerContactId)
   at Mediachase.Commerce.Customers.Handlers.ContactRequestHandler.PreDelete(BusinessContext context)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Execute(Request request)
   at Mediachase.BusinessFoundation.Data.Business.BusinessManager.Delete(String metaClassName, PrimaryKeyId primaryKeyId)


Have any of you encountered such an issue? If so, how did you solve it?

#306361
Aug 07, 2023 10:32
Vote:
 

You should not have to set the PreferredBillingAddressId  and PreferredShippingAddressId to empty before deleting. why do you do that? 

#306362
Aug 07, 2023 11:35
Vote:
 

I did this because I traced the code in Commerce Manager, and there's a piece of code there:

Of course, I also tried deleting a user without changing the PreferredBillingAddressId and PreferredShippingAddressId, but the result is the same. I still receive a Null Exception.

#306368
Aug 07, 2023 11:42
Vote:
 
I found a workaround by overwriting the UserId.

public bool DeleteUser(CustomerContact contact, MapUserKey mapUserKey)
{
var userId = mapUserKey.ToUserKey(contact?.UserId);
if (Membership.DeleteUser(userId.ToString()))
{
OverwriteUserId(contact);
BusinessManager.Delete(StringConstants.CustomFields.ContactClassName, (PrimaryKeyId)contact.PrimaryKeyId);
return true;
}
return false;
}
private void OverwriteUserId(CustomerContact contact)
{
contact.UserId = Guid.NewGuid().ToString();
contact.SaveChanges();
}
#309604
Edited, Sep 27, 2023 11:41
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.