December 20, 2006

How to change status of the dynamic entity?

This feature is not well documented, so I decided to describe it here:

//Create a request object
SetStateDynamicEntityRequest stateRequest = new SetStateDynamicEntityRequest();

//Use string and not integer value for state. (For Lead use "Qualified" and not 2)
stateRequest.State = newState;
state
Request.Status = newStatus;

//Create an instance of the entity
Moniker m = new Moniker();
m.Id = entityId;
m.Name = myDynamicEntity.Name;

//Set the state. Well... set the entity.
state
Request.Entity = m;

//Send the request
SetStateDynamicEntityResponse res = (SetStateDynamicEntityResponse)_crmService.Execute(state
Request);


Pretty simple, huh?

For a complete list of state and status values take a look in the SDK. You should find an enumeration for every built-in entity state together with allowed status reasons.
The name should be State Enumeration (LeadState Enumeration). Custom entities state is "Active" or "Inactive".


Of course you can use strong typed entities: (but I prefer dynamic ones)

lead myLead = new lead();
Status statusCode = new Status();
statusCode.Value = 3;
myLead.statecode = LeadState.Qualified;
myLead.statuscode = statusCode;