CRM Blog

Example Of Deactivating An Entity

by Danny Varghese 03.06.09

One of the least published example of CRM code is deactivating an entity, probably because it's not used as often as creating, updating or retrieving entities.  Below is an example I've used on several occasions to deactivate an entity:

 

public void DeactivateEntity(Guid entityId)

{

//variable initialization

            SetStateDynamicEntityRequest deactivateReq = new SetStateDynamicEntityRequest();   

 

            //deactivate the cloned assignment

            deactivateReq = new SetStateDynamicEntityRequest();

            deactivateReq.State = "Inactive";

            deactivateReq.Status = 2;

            deactivateReq.Entity = new Moniker();

            deactivateReq.Entity.Name = <entity name>

            deactivateReq.Entity.Id = entityId;

 

//execute the deactivation request

            service.Execute(deactivateReq);

}

Filed under:

Comments

# Jodi Cannon said on April 16, 2009 10:53 AM:

Is there a way to detect the account deactivation process from a view?  I need to prevent deactivation based upon role.  

# Danny Varghese said on April 16, 2009 11:10 AM:

Hi Jodi,

There's a few avenues you can take here, and let you decide which is best suited for your business needs.  

1.  You can always restrict update access to the entity with out of the box CRM security roles  i.e. for that specific security role, limit the update ability on the account.

2.  There is no view you can create, but there is a view that will show you all "deactivated" accounts out of the box and it will tell you who last updated it giving you some indication who deactivated it.  The view name is "Inactive Accounts."  The downside is this view shows records AFTER it's been deactivated.

3.  If you want to allow the user to update the record but prevent the user from deactivating the plug-in in real time, then you'll need to write a plug-in.  The plug-in can be fired on a pre-event to see if the entity is being deactivated either re-activate it or just stop it all together.

4.  The last option is there is third party software out there that you can purchase to do this.

I hope this helps, please let me know if you want me to clarify something =).  Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Security Check
Please answer the simple math problem below.

(required)