jimminybob
jimminybob

Reputation: 1442

Error when trying to delete record on deactivate

I have a record that when deactivated I want a plugin to automatically delete the record. So I have my code set to the SetStateDynamically event and it all works up to the point where I actually get the service to delete the record, where I get "An unexpected error occurred".

My code is below, can anyone see what the problem might be, or be able to point me in the right direction?

Thanks

private void MarkForDeleteDeactivatedRole(EntityReference entity)
    {
        Entity role = Service.Retrieve("sb_contactsecurityrole", entity.Id, new ColumnSet(true));

        OptionSetValue statusValue = (OptionSetValue)role["statecode"];

        if (statusValue.Value == 1)
        {                
            Service.Delete(entity.LogicalName, entity.Id);
        }
    }

Upvotes: 0

Views: 249

Answers (1)

John Hoven
John Hoven

Reputation: 4085

I've seen the same. I don't think the CRM event pipeline supports a record being deleted while it is working on that object. The best you're probably going to be able to do is register your plug-in asynchronously, so the error (which you can't catch, because it is in the CRM event pipeline internals) doesn't bubble to the UI (and you're outside of the transaction).

Upvotes: 1

Related Questions